android - Support Map fragment inside list item adapter returning null second time -


i using fragments in app.first 1 activity loaded , fragments come on activity , destroy.my problem here trying load support mapfragment inside listview adapter when clicked on button inside list item,it works fine first time second time

        gmap = ((supportmapfragment) fm.findfragmentbyid(r.id.map)).getmap(); 

this line returning null inside adapter.what should rectify problem here. calling map fragment inside adapter in oncreateviewholder method of adapter.

public class productlistadapter extends recyclerview.adapter {

private activity context; list<listdetails> products; sharedpreference sharedpreference; public static com.android.volley.toolbox.imageloader imageloader; layoutinflater inflater; viewholder holder; protected string day = ""; public static view alertlayout; private static gmapv2direction gmapdirection;  public static  googlemap gmap;  protected document doc;   public productlistadapter(context context, list<listdetails> products) {          this.context = (activity)context;         this.products = products;         sharedpreference = new sharedpreference();         imageloader = appcontroller.getinstance().getimageloader();           inflater = (layoutinflater) context                 .getsystemservice(activity.layout_inflater_service);      }   @override public long getitemid(int position) {     return position; }    public static class contactviewholder extends recyclerview.viewholder {     textview productnametxt;     textview ratingsno;     textview productpricetxt;      textview timedist;     tablelayout distancesection;     textview categoryname;     imageview favoriteimg;     ratingbar rb;      imageview offerimage;     imageview mapimage;      networkimageview listimage;     public textview distance;     public imageview indicator;     textview timer;     textview postedon;           public contactviewholder(view v) {        super(v);        productnametxt = (textview) v                 .findviewbyid(r.id.name);          productpricetxt = (textview) v                 .findviewbyid(r.id.listview_description);         distance = (textview) v                 .findviewbyid(r.id.listview_distane);         timedist = (textview) v                 .findviewbyid(r.id.timedist);         ratingsno = (textview) v                 .findviewbyid(r.id.ratingsno);           postedon = (textview) v                 .findviewbyid(r.id.postedon);         favoriteimg = (imageview) v                 .findviewbyid(r.id.fav_checkbox);          rb = (ratingbar) v                 .findviewbyid(r.id.ratingbar);         timer = (textview) v.findviewbyid(r.id.time);         categoryname = (textview) v                 .findviewbyid(r.id.categoryname);         listimage = (networkimageview) v                 .findviewbyid(r.id.list_image);         distancesection = (tablelayout) v                 .findviewbyid(r.id.distancesection);           mapimage = (imageview) v                 .findviewbyid(r.id.imgmap);        }   }  @override public int getitemcount() {     return products.size(); }  @override public void onbindviewholder(contactviewholder contactviewholder, final int position) {      try {           string text = "<font color=#222222>"+products.get(position).getshopname()+"</font>"+"<small><font color=#0091ea> ( </font></small>"+" <small><font color=#0091ea>"+products.get(position).getareaname()+"</font></small>"+"<small><font color=#0091ea> ) </font></small>";         contactviewholder.productnametxt.settext(html.fromhtml(text));          //holder.productnametxt.settext(product.getshopname() + "  " + "( " + html.fromhtml("<font color='#0091ea'>" + product.getareaname() + "</font>") + " )");         contactviewholder.productpricetxt.settext(products.get(position).getdeal() + "");         contactviewholder.rb.setrating(float.valueof(products.get(position).getratingavg()));         contactviewholder.ratingsno.settext(products.get(position).gettotalratings() + " ratings");          contactviewholder.timedist.settext(products.get(position).gettime());         contactviewholder.distance.settext(string.valueof(products.get(position).getdistance())+ " km");         contactviewholder.categoryname.settext(products.get(position).getcategoryname());         contactviewholder.listimage.setimageurl(products.get(position).getlistimage(), imageloader);            contactviewholder.mapimage.setonclicklistener(new onclicklistener() {               @override             public void onclick(view v) {                 try {                       final dialog dialog = new dialog(context);                       if (alertlayout != null) {                          viewgroup parentviewgroup = (viewgroup) alertlayout                                  .getparent();                          if (parentviewgroup != null) {                              parentviewgroup.removeview(alertlayout);                          }                      }                          //dialog.setcontentview(r.layout.list_map_dialog);                       dialog.requestwindowfeature(window.feature_no_title);                      dialog.setcontentview(alertlayout);                         // dialog.settitle("map directions");                             imageview cancel = (imageview) dialog                                 .findviewbyid(r.id.cancel);                           cancel.setonclicklistener(new view.onclicklistener() {                              @override                             public void onclick(view arg0) {                                 dialog.cancel();                             }                          });                          dialog.getwindow().settype(windowmanager.layoutparams.type_system_alert);                          dialog.show();                           loadplacedetails(products.get(position).getlat(),products.get(position).getlng(),products.get(position).getareaname(),products.get(position).getcategoryname());                     } catch (exception e) {                         e.printstacktrace();                                     postlogcaterrors ple = new postlogcaterrors();                     ple.postlogcaterorrs(e);                 }             }         });          if (checkfavoriteitem(products.get(position))) {             contactviewholder.favoriteimg.setimageresource(r.drawable.checked);             contactviewholder.favoriteimg.settag(context.getstring(r.string.red_favcolor));         } else {             contactviewholder.favoriteimg.setimageresource(r.drawable.unchecked);             contactviewholder.favoriteimg.settag(context.getstring(r.string.grey_favcolor));         }         contactviewholder.postedon.settext("posted on" + "  " + products.get(position).getposteddate());          contactviewholder.timer.settext(products.get(position).getofferendtime());         } catch (exception e) {         // todo auto-generated catch block         e.printstacktrace();     }  }    @override public contactviewholder oncreateviewholder(viewgroup arg0, int arg1) {      view itemview = layoutinflater.              from(arg0.getcontext()).              inflate(r.layout.list_item4, arg0, false);       layoutinflater li = (layoutinflater) context                 .getsystemservice(context.layout_inflater_service);         //here adding map layout        if (alertlayout == null)             alertlayout = li.inflate(r.layout.list_map_dialog, null);       return new contactviewholder(itemview); }  public boolean checkfavoriteitem(listdetails checkproduct) {     boolean check = false;     list<listdetails> favorites = sharedpreference.getfavorites(context);     if (favorites != null) {         (listdetails product : favorites) {             if (product.equals(checkproduct)) {                 check = true;                 break;             }         }     }     return check; }  private void loadplacedetails(double destlat, double destlng, string areaname,string category) {     try {         latlng fromposition = new latlng(splashscreen.nwlocation.getlatitude(), splashscreen.nwlocation.getlongitude());          latlng toposition = new latlng(destlat, destlng);          gmapdirection = new gmapv2direction();          fragmentmanager fm = ((fragmentactivity) context)                 .getsupportfragmentmanager();          //here loading map after click on map button           gmap = ((supportmapfragment) fm.findfragmentbyid(r.id.map)).getmap();             gmap.clear();         gmap.animatecamera(cameraupdatefactory.newlatlngzoom(fromposition,13));          gmap.addmarker(new markeroptions()                 .position(fromposition)                 .title("current location")                 .icon(bitmapdescriptorfactory                         .defaultmarker(bitmapdescriptorfactory.hue_orange))).showinfowindow();;                          switch (category) {                         case "book_store":                              gmap.addmarker(new markeroptions()                                 .position(toposition)                                 .title(areaname)                                 .icon(bitmapdescriptorfactory.frombitmap(writetextondrawable(r.drawable.ic_book, ""))));                             break;                         case "beauty_salon":                              gmap.addmarker(new markeroptions()                                 .position(toposition)                                 .title(areaname)                                 .icon(bitmapdescriptorfactory.frombitmap(writetextondrawable(r.drawable.ic_fashion, ""))));                              break;                         case "electronics_store":                              gmap.addmarker(new markeroptions()                                 .position(toposition)                                  .title(areaname)                                 .icon(bitmapdescriptorfactory.frombitmap(writetextondrawable(r.drawable.ic_electronic, ""))));                             break;                         case "food":                              gmap.addmarker(new markeroptions()                                 .position(toposition)                                 .title(areaname)                                 .icon(bitmapdescriptorfactory.frombitmap(writetextondrawable(r.drawable.ic_food, ""))));                             break;                         case "home_goods_store":                              gmap.addmarker(new markeroptions()                                 .position(toposition)                                 .title(areaname)                                 .icon(bitmapdescriptorfactory.frombitmap(writetextondrawable(r.drawable.ic_home, ""))));                             break;                         case "restaurant":                              gmap.addmarker(new markeroptions()                                 .position(toposition)                                 .title(areaname)                                 .icon(bitmapdescriptorfactory.frombitmap(writetextondrawable(r.drawable.ic_job, ""))));                             break;                         case "real_estate_agency":                              gmap.addmarker(new markeroptions()                                 .position(toposition)                                 .title(areaname)                                 .icon(bitmapdescriptorfactory.frombitmap(writetextondrawable(r.drawable.ic_realestate, ""))));                             break;                         case "travel_agency":                              gmap.addmarker(new markeroptions()                                 .position(toposition)                                 .title(areaname)                                 .icon(bitmapdescriptorfactory.frombitmap(writetextondrawable(r.drawable.ic_vehicle, ""))));                             break;                     }            document doc = gplaces.getdocument(fromposition, toposition,                 gmapv2direction.mode_driving,gmapdirection,gmap, context);      } catch (numberformatexception e) {         e.printstacktrace();                     postlogcaterrors ple = new postlogcaterrors();         ple.postlogcaterorrs(e);     }  } 

}

am doing wrong here? suggest me on this..thanks in anticipation

probably deleting supportmapfragment inside r.id.map. show little code know wrong.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -