android 膨胀 class 片段时出错

android Error inflating class fragment

我开发了一个应用程序,因为我必须显示地图。我从 api-网络服务获得的纬度和经度。

我有一个自定义基础适配器来显示所有数据的原始文件。

我剩下的数据集很好

但是当我在原始文件中使用片段时,出现错误。

这一行,

     vi = inflater.inflate(R.layout.raw_file, null);

也尝试通过 :

      vi = inflater.inflate(R.layout.chat_raw_3, parent , true); 

      vi = inflater.inflate(R.layout.chat_raw_3, parent , false);

      vi = inflater.inflate(R.layout.chat_raw_3, null, false);

在 getView(...) 方法中。

在 raw_file.xml 文件中,我添加了如下片段:

     <fragment
            android:id="@+id/map_sender"
            android:layout_width="150dp"
            android:layout_height="150dp"
            class="com.google.android.gms.maps.SupportMapFragment"
      />

我在 logcat 中遇到以下错误:

     android.view.InflateException: Binary XML file line #113: Error   inflating class fragment
        at   android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
     .
     .
     .
     Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.fragment" on path: DexPathList

重要的是它在我的另一个 activity(未使用膨胀原始文件)上运行良好,但在使用自定义适配器原始文件时出现上述错误.

我在网上搜索了很多选项,但没有得到解决方案,所以请任何人帮助我解决这个问题。

编辑

我的整个 Class 结构如下:

 public class My_Activity extends ListActivity {

  // all variable declaration here

   @Override
  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_screen);
    initialize();

  }

  private void initialize()
  {
        // all fields initialize here

        // set value to custom adapter
        new setAdapter().execute();

  }

  public class setAdapter extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();


    }

    @Override
    protected String doInBackground(String... params) {

        // other things
        return "yes";
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        try {
            adpt = new MyAdapter(getApplicationContext(), arr);
            setListAdapter(adpt);
            adpt.notifyDataSetChanged();
            getListView().setSelection(adpt.getCount() - 1);

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
private class MyAdapter extends BaseAdapter implements LocationListener  {
 ArrayList<HashMap<String, String>> arr;
 HashMap<String, String> map;
 Context c;
 LayoutInflater inflater;
 private GoogleMap map_sender, map_reciver;
 double latitude;
 double longitude;

public MyAdapter(Context activity, ArrayList<HashMap<String, String>> arr) {

    inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    c = activity;
    arr = arr;
}

@Override
public int getCount() {
    return arr.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)

   // I got error in following line ========== error below line =======
        vi = inflater.inflate(R.layout.raw_file, null);

    map_sender = ((MapFragment) getFragmentManager().findFragmentById(R.id.map_sender))
            .getMap();

    TextView tv_room_name = (TextView) vi.findViewById(R.id.tv_room_name);
    TextView tv_raw_date = (TextView) vi.findViewById(R.id.tv_raw_date);

    HashMap<String, String> product = new HashMap<String, String>();
    product = arr.get(position);


    String fiile_type_locations = product.get("file_type_location").toString();
    String file_beam_latitudes = product.get("file_latitude").toString();
    String file_beam_longitudes = product.get("file_longitude").toString();

    tv_room_name.setVisibility(View.VISIBLE);
    tv_room_name.setText("");

   if (!fiile_type_locations.equals("")) {

        // here i set map
    }
    return vi;
}

@Override
public void onLocationChanged(Location location) {

}

}

我的 raw_file.xml 如下所示:

  <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="wrap_content">


      <LinearLayout
    android:id="@+id/rl_raw_date"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:visibility="visible">

    <TextView
        android:id="@+id/tv_raw_date"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:gravity="center"
        android:text="Date" />

    <TextView
        android:id="@+id/tv_room_name"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_marginLeft="10dp"
        android:singleLine="true"
        android:text="senderName"
        android:textSize="14sp"
        android:visibility="gone" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/contentWithBackground_sender_location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:orientation="vertical"
        android:paddingBottom="10dp"
        android:paddingRight="10dp"
        android:visibility="gone">

      <!-- binary xml file inflating following first line -->

       <fragment
            android:id="@+id/map_sender"
            android:layout_width="150dp"
            android:layout_height="150dp"
            class="com.google.android.gms.maps.MapFragment"
            />
       <!-- <fragment
            android:id="@+id/map_sender"
            class="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="150dp"
            android:layout_height="150dp" />-->

    </LinearLayout>
 </LinearLayout>

使用

map_sender = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_sender)).getMap();

私有上下文activity; // 全局 然后

public MyAdapter(Context activity, ArrayList<HashMap<String, String>> arr) {


    this.activity = activity;
    arr = arr;
}

已编辑答案

然后传递你的LayoutInflater

 LayoutInflater inflater=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if(convertView == null){
     convertView = inflater.from(activity).inflate(R.layout.raw_file, null);

这个问题你有两个备选方案:

第一个替代解决方案: 使用 Imageview 而不是 fragment 并将静态映射 url 设置为该图像。最好的办法,而且负载也不大

第二种解决方案:使用 MapView 而不是 fragment。

第一个解决方案说明:

使用 Map Static Url 将静态离线地图显示到 imageview。

 url = "http://maps.google.com/maps/api/staticmap?center=" + Latitude + "," + Longitude + "&zoom=15&size=200x200&sensor=false"

在 raw_file 中使用 Imageview 而不是像

这样的片段
   <ImageView
            android:id="@+id/your_imageview"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:maxWidth="250dp"
            android:src="@drawable/ic_launcher"
            android:textColor="@android:color/white"
            android:visibility="visible" />

并使用 ImageLoader 在 java 文件中使用:

1)  ImageLoader imageLoader;
2)  imageLoader = new ImageLoader(Your_Activity.this);
3)  imageLoader.DisplayImage("http://maps.google.com/maps/api/staticmap?center=" + Latitide + "," + Longitude + "&zoom=15&size=200x200&sensor=false", your_imageview);

简单结束::)

第二种方式说明:

在 raw_file

中使用 Mapview 而不是片段

喜欢以下内容:

 <com.google.android.gms.maps.MapView
            android:id="@+id/mapview"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:visibility="gone"/>

并且在您的 java 文件中:

1)  MapView mapView;
    GoogleMap map

2)  mapView = (MapView) vi.findViewById(R.id.mapview);

3)   mapview_receiver.onCreate(savedInstanceState);

                // Gets to GoogleMap from the MapView and does initialization stuff
                map_recive = mapview_receiver.getMap();
                map_recive.getUiSettings().setMyLocationButtonEnabled(false);
                map_recive.setMyLocationEnabled(true);

                // Needs to call MapsInitializer before doing any CameraUpdateFactory calls
                try {
                    MapsInitializer.initialize(Chat_Screen_Activity.this);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                map_recive.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 3));
                // Zoom in, animating the camera.
                map_recive.animateCamera(CameraUpdateFactory.zoomTo(6), 100, null);

好的,你的工作完成了!!!

但与第一张图片 URL(静态离线地图)相比,方式是最好的,点击后您在另一个 activity 上显示地图。

当你滚动你的列表视图时它不会加载,并且在没有加载的情况下以尽可能快的速度显示,所以这是最好的方法。