android - Show ListPopupWindow from PopWindow -


i have popupwindow button. on click on button want show listpopupwindow get

fatal exception: main android.view.windowmanager$badtokenexception: unable add window -- token android.view.viewrootimpl$w@439c79f0 not valid; activity running? 

and show() method called onclick(). error on last line

popup.show(); 

full:

void showlist(view view){      final listpopupwindow popup = new listpopupwindow(this);     popup.setadapter(new arrayadapter<string>(this,             android.r.layout.simple_list_item_1, category));     popup.setanchorview(view);     popup.setwidth(listpopupwindow.wrap_content);     popup.setonitemclicklistener(new adapterview.onitemclicklistener() {          @override         public void onitemclick(adapterview<?> parent, view view,                                 int position, long id) {             tvcategory.settext(category[position]);             popup.dismiss();         }     });      popup.show();   } 

fulllactivity code:

public class mapsights extends activity implements onmapreadycallback {  imageview btnplus, close_add_sight; popupwindow pw, add_sg; popupmenu popupmenucategory; listpopupwindow listpopupwindow; int 1 = 1; textview tvcategory; linearlayout add_sight_btn; context context; string tag = "mapsights.java";  private static final string[] category = { "amphibian",         "fish", "reptile", "bird","mammal",         "miriapode", "insect", "arachnid","shellfish",         "snail", "shell", "slime mold","fungi",         "plant", "anemone", "coral"};   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.map_sights);      log.d(tag, "oncreate");       context = getapplicationcontext();      mapfragment mapfragment = (mapfragment) getfragmentmanager().findfragmentbyid(r.id.map);     mapfragment.getmapasync(this);      layoutinflater inflater = (layoutinflater) mapsights.this.getsystemservice(context.layout_inflater_service);     view pview = inflater.inflate(r.layout.maplusbtnadds, null, false);     view pviewsight = inflater.inflate(r.layout.add_sighting, null, false);     tvcategory = (textview) pviewsight.findviewbyid(r.id.txt_category);       pw = new popupwindow(pview, 450, 650, false);     add_sg = new popupwindow(pviewsight, 450, 650, false);       btnplus = (imageview) findviewbyid(r.id.plusbtn);     close_add_sight = (imageview) pview.findviewbyid(r.id.close_plus);      btnplus.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             pw.showatlocation(mapsights.this.findviewbyid(r.id.map), gravity.bottom | gravity.right, 0, 0);         }     });      close_add_sight.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             pw.dismiss();         }     });      add_sight_btn = (linearlayout) pview.findviewbyid(r.id.add_sight_btn);     add_sight_btn.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             add_sg.showatlocation(mapsights.this.findviewbyid(r.id.map), gravity.bottom | gravity.right, 0, 0);         }      });       tvcategory.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             showlist(v);         }     });  }   @override protected void onresume() {     super.onresume();     log.d(tag,"onresume"); }   @override protected void onstart() {     super.onstart();     log.d(tag,"onstart"); }   @override protected void onstop() {     super.onstop();     log.d(tag,"onstop"); }   void showlist(view view){      final listpopupwindow popup = new listpopupwindow(this);     popup.setadapter(new arrayadapter<string>(this,             android.r.layout.simple_list_item_1, category));     popup.setanchorview(view);     popup.setwidth(listpopupwindow.wrap_content);     popup.setonitemclicklistener(new adapterview.onitemclicklistener() {          @override         public void onitemclick(adapterview<?> parent, view view,                                 int position, long id) {             toast.maketext(mapsights.this, "clicked item " + position, toast.length_short).show();             tvcategory.settext(category[position]);             popup.dismiss();         }     });      popup.show();   }   @override public void onmapready(googlemap map) { 

// map.addmarker(new markeroptions().position(new latlng(0, 0)).title("marker")); map.setmaptype(map_type_terrain); }

}

change final listpopupwindow popup = new listpopupwindow(this); final listpopupwindow popup = new listpopupwindow(mapsights.this);

try check if activity finishing before call popup.show(); avoid error is activity running?

if(!isfinishing()) {     popup.show(); } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -