0

I developed one app , in that i have to display map. latitude and longitude i got from the api-web-service.

I that have one Custom base adapter to display raw file of all data.

My remaining data set very well.

But when i used fragment in my raw file , i got the error.

in this line,

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

also trying it by :

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

or

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

or

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

in getView(...) method.

in raw_file.xml file i add fragment like following :

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

I got following error in 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

The important thing is that it work fine on my another activity ( that not used inflate raw file ) but while using custom adapter raw file it giving above error.

I search many option online, but not got the solution, So please any one help me to solve-out this question.

EDIT

My Entire Class Structure Like following :

 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) {

}

}

My raw_file.xml is look like following :

  <?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>
Joseph Mekwan
  • 1,082
  • 1
  • 15
  • 28

2 Answers2

2

You Have Two alternative solutions of this question:

First alternative Solutions : Use Imageview instead of fragment and set static map url to that image. It is best way , and also not load much.

Second solutions : Use MapView instead of fragment.

First Solution Description :

Use Map Static Url to show static offline map to imageview.

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

Use Imageview in your raw_file instead of fragment like

   <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" />

And Use in java file using ImageLoader :

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);

Simple its over : :)

Second Way Description :

Use Mapview instead of fragment in your raw_file

like following :

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

And in your java file :

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);

Ok, its done your work done!!!

But compare to both First Image URL (static offline map) way is best , after click on that you display map on another activity also.

Its not load when you scroll your listview and display with as much speed without loading, So its best way.

Nirav Mehta
  • 1,715
  • 4
  • 23
  • 42
1

Use

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

private Context activity; // Global Then

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


    this.activity = activity;
    arr = arr;
}

Edited Answer

Then pass your LayoutInflater

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

if(convertView == null){
     convertView = inflater.from(activity).inflate(R.layout.raw_file, null);
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198