android - Unable to add Window, Token is not valid error when clicked on a Spinner -
i have android application when clicked on option side bar goes fragment, , fragment has clickable radio buttons. when clicked on these create popup window text fields in it.
basically how flow goes,
activity --> fragment 1 --> fragment 2 --> popupwindow
and have spinner on popupwindow, when click on select value throws following exception. don't understand why happen.
process: com.informaticsint.claimassistant, pid: 5045 android.view.windowmanager$badtokenexception: unable add window -- token android.view.viewrootimpl$w@945936c not valid; activity running? @ android.view.viewrootimpl.setview(viewrootimpl.java:849) @ android.view.windowmanagerglobal.addview(windowmanagerglobal.java:337) @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:91) @ android.widget.popupwindow.invokepopup(popupwindow.java:1329) @ android.widget.popupwindow.showasdropdown(popupwindow.java:1155) @ android.widget.listpopupwindow.show(listpopupwindow.java:791) @ android.widget.spinner$dropdownpopup.show(spinner.java:1366) @ android.widget.spinner.performclick(spinner.java:828) @ android.view.view$performclick.run(view.java:22526) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:158) @ android.app.activitythread.main(activitythread.java:7224) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1230) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1120)
this spinner code cause problem. in below mentioned assignmentdetailsactivity class, showdamageditementrypopup() method
statusspinner = (spinner)popupview.findviewbyid(r.id.popup_status_spinner); arrayadapter<string> statusspinnerarrayadapter = new arrayadapter<string>(assignmentdetailsactivity.this, android.r.layout.simple_spinner_item, statusspinnerarray); statusspinnerarrayadapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); statusspinner.setadapter(statusspinnerarrayadapter);
this method creates popup in assignmentdetailsactivity class
public void showdamageditementrypopup(radiobutton radiobutton, view view){ layoutinflater layoutinflater = (layoutinflater)getbasecontext().getsystemservice(context.layout_inflater_service); view popupview = layoutinflater.inflate(r.layout.component_selection_popup, null); final popupwindow popupwindow = new popupwindow( popupview, viewgroup.layoutparams.wrap_content, viewgroup.layoutparams.wrap_content); // set popup animation style popupwindow.setanimationstyle(r.style.popupanimation); button buttonclose = (button)popupview.findviewbyid(r.id.close_add_component_btn); // close button damaged item popop window buttonclose.setonclicklistener(new button.onclicklistener(){ @override public void onclick(view v) { popupwindow.dismiss(); } }); originalamount = (edittext)popupview.findviewbyid(r.id.popup_add_component_original_amount); customercontribution = (edittext)popupview.findviewbyid(r.id.popup_percentage); quantity = (edittext)popupview.findviewbyid(r.id.popup_quantity); finalamount = (edittext)popupview.findviewbyid(r.id.popup_add_component_final_amount); remarks = (edittext)popupview.findviewbyid(r.id.popup_add_component_remarks); // item spinner itemspinnerarray = new arraylist<string>(); itemspinnerarray.add("select item"); // status spinner arraylist<string> statusspinnerarray = new arraylist<string>(); statusspinnerarray.add("fdr"); statusspinnerarray.add("dr"); statusspinnerarray.add("sp"); damagecomponenetautocompletetextview = (autocompletetextview) popupview.findviewbyid(r.id.popup_damage_component_item); damagecomponenetautocompletetextview.requestfocus(); arrayadapter<string> itemspinnerarrayadapter = new arrayadapter<string>(this, android.r.layout.simple_spinner_dropdown_item, itemspinnerarray); damagecomponenetautocompletetextview.setthreshold(1); damagecomponenetautocompletetextview.setadapter(itemspinnerarrayadapter); damagecomponenetautocompletetextview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { itemspinnervalue = (string) parent.getitematposition(position); log.d("sk-->", "----------------------------------------------------------"); log.d("sk-->","itemspinnervalue: " + itemspinnervalue); } }); statusspinner = (spinner)popupview.findviewbyid(r.id.popup_status_spinner); arrayadapter<string> statusspinnerarrayadapter = new arrayadapter<string>(assignmentdetailsactivity.this, android.r.layout.simple_spinner_item, statusspinnerarray); statusspinnerarrayadapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); statusspinner.setadapter(statusspinnerarrayadapter); //creating text watcher textwatcher textwatcher = new textwatcher() { @override public void beforetextchanged(charsequence charsequence, int i, int i1, int i2) { } @override public void ontextchanged(charsequence charsequence, int i, int i1, int i2) { } @override public void aftertextchanged(editable editable) { //here, after introduced in edittext string //string answerstring = originalamount.gettext().tostring(); if (originalamount.gettext().tostring().trim().equals("") || customercontribution.gettext().tostring().trim().equals("") || quantity.gettext().tostring().trim().equals("")) { // error , 1 or more edittext empty } else { calculatefinalamount(); } //and make toast //modify "youractivity.this" activity name .this //toast.maketext(youractivity.this,"the string edittext is: "+answerstring,0).show(); } }; // adding text watcher our text boxes originalamount.addtextchangedlistener(textwatcher); customercontribution.addtextchangedlistener(textwatcher); quantity.addtextchangedlistener(textwatcher); // show popup popupwindow.showatlocation(view, gravity.center, 0, 0); } public void onsaveitem(view view) { statusspinnervalue = (string) statusspinner.getitematposition(statusspinner.getselecteditemposition()); statusspinnervalue = "abc"; itemspinnervalue = "test item"; originalamount.settext("50"); customercontribution.settext("25"); quantity.settext("1"); if(itemspinnervalue.matches("select item") ||itemspinnervalue.matches("") || statusspinnervalue.matches("") || originalamount.gettext().tostring().matches("") || customercontribution.gettext().tostring().matches("") || quantity.gettext().tostring().matches("")){ alertdialog.builder builder = new alertdialog.builder(this); builder.settitle("error!"); builder.setmessage("please fill required fields.") .setcancelable(false) .setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { //do things dialog.dismiss(); } }); alertdialog alert = builder.create(); alert.show(); } else{ log.e("test", "check passed"); date date = new date(); if(mdbhandler.itemalreadyexist(reportid,"item_name", itemspinnervalue, "damagecomponent") == false){ mdbhandler.adddamagecomponent(reportid, itemspinnervalue, statusspinnervalue, originalamount.gettext().tostring(), double.parsedouble(customercontribution.gettext().tostring()), integer.parseint(quantity.gettext().tostring()), finalamount.gettext().tostring(), remarks.gettext().tostring()); mdbhandler.updatereport(reportid, date.tostring(), "time_last_modified"); toast.maketext(this,"component added",toast.length_short).show(); } else{ alertdialog.builder builder = new alertdialog.builder(this); builder.settitle("error!"); builder.setmessage("item exist.") .setcancelable(false) .setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { //do things dialog.dismiss(); } }); alertdialog alert = builder.create(); alert.show(); } mdbhandler.close(); } }
spent 2 days same problem :(
the workaround find use spinner in dialog mode
android:spinnermode="dialog"
Comments
Post a Comment