java - android.view.WindowManager$BadTokenException: Unable to add window — token null is not valid -
i error whenever try start window class. using separate class, , not method within game class, cause need disable button on popup window. calling class button. code works fine if use within game class, not in separate class. here code:
public class popup_pogresno extends activity implements onclicklistener{ private popupwindow pwindow; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); layoutinflater layoutinflater = (layoutinflater)popup_pogresno.this .getsystemservice(layout_inflater_service); view popupview = layoutinflater.inflate(r.layout.popup, null); pwindow = new popupwindow(popupview, 300, 170, true); button btndismiss = (button)popupview.findviewbyid(r.id.bpopupok); btndismiss.setonclicklistener(new button.onclicklistener(){ public void onclick(view v) { // todo auto-generated method stub pwindow.dismiss(); }}); pwindow.showatlocation(popupview, gravity.center, 0, 0); } public void onclick(view v) { // todo auto-generated method stub } @override public void onbackpressed() { } }
you not calling setcontentview(r.layout.mylayout)
in oncreate(bundle)
method. call right after super.oncreate(savedinstancestate);
.
this activity resource page on android developers site:
there 2 methods subclasses of activity implement:
oncreate(bundle) initialize activity. importantly, here call setcontentview(int) layout resource defining ui, , using findviewbyid(int) retrieve widgets in ui need interact programmatically.
onpause() deal user leaving activity. importantly, changes made user should @ point committed (usually contentprovider holding data).
edit 1:
replace:
pwindow.showatlocation(popupview, gravity.center, 0, 0);
with:
new handler().postdelayed(new runnable(){ public void run() { pwindow.showatlocation(popupview, gravity.center, 0, 0); } }, 100l);
Comments
Post a Comment