popupwindow - android.view.WindowManager$BadTokenException: Unable to add window -
i'm adding popup window in fragment when user clicks on floating action button, in popup window have added autocomplete edit field when clicked floating action button, popup window shows when click edit field gave error. error
here complete error.
process: com.example.mubtadanaqvi.stunexus, pid: 7252 android.view.windowmanager$badtokenexception: unable add window -- token android.view.viewrootimpl$w@4234e238 not valid; activity running? @ android.view.viewrootimpl.setview(viewrootimpl.java:769) @ android.view.windowmanagerglobal.addview(windowmanagerglobal.java:278) @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:69) @ android.widget.popupwindow.invokepopup(popupwindow.java:1067) @ android.widget.popupwindow.showasdropdown(popupwindow.java:966) @ android.widget.listpopupwindow.show(listpopupwindow.java:635) @ android.widget.autocompletetextview.showdropdown(autocompletetextview.java:1119) @ android.widget.autocompletetextview.updatedropdownforfilter(autocompletetextview.java:973) @ android.widget.autocompletetextview.onfiltercomplete(autocompletetextview.java:955) @ android.widget.filter$resultshandler.handlemessage(filter.java:285) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:146) @ android.app.activitythread.main(activitythread.java:548) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1283) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1099) @ dalvik.system.nativestart.main(native method)
here code
floatingactionbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { layoutinflater inflater = (layoutinflater) getcontext().getsystemservice(layout_inflater_service); final view customview = inflater.inflate(r.layout.add_new_skill, null); mpopupwindow = new popupwindow( customview, viewgroup.layoutparams.match_parent, viewgroup.layoutparams.wrap_content, true ); imagebutton cancel = (imagebutton) customview.findviewbyid(r.id.close_button); cancel.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { mpopupwindow.dismiss(); } }); skilllistadapter = new arrayadapter<>(getcontext(),android.r.layout.simple_dropdown_item_1line, skillslist); skillinputfield = (autocompletetextview) customview.findviewbyid(r.id.skill_input); skillinputfield.setthreshold(1); skillinputfield.setadapter(skilllistadapter); skilllistadapter.addall(skillslist); skilllistadapter.notifydatasetchanged(); button savebutton = (button) customview.findviewbyid(r.id.save_skill); savebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { string skilltext = skillinputfield.gettext().tostring(); if (skilltext.isempty()) maketoast("empty"); else { if (connectivitylistener.isnetwork(getcontext())) { // progressdialog.show(); mpopupwindow.dismiss(); maketoast(skilltext); list.add(new skill_item(skilltext, 0)); adapter.notifydatasetchanged(); } else maketoast("connection error!"); } } }); mpopupwindow.showatlocation(rootlayout, gravity.center, 0, 0); } });
reason of exception: activity has finished trying display dialog context of finished activity. since there no window dialog display android runtime throws exception.
solution of exception: please use isfinishing() method called android check whether activity in process of finishing:
private final weakreference<mainactivity> mainactivityweakref; mainactivityweakref =new weakreference<mainactivity>(this); if (mainactivityweakref.get() != null && !mainactivityweakref.get().isfinishing()) { // code }
Comments
Post a Comment