popupwindow - Problems creating a Popup Window in Android Activity -
i'm trying create popup window appears first time application starts. want display text , have button close popup. however, i'm having troubles getting popupwindow work. i've tried 2 different ways of doing it:
first have xml file declares layout of popup called popup.xml (a textview inside linearlayout) , i've added in oncreate() of main activity:
popupwindow pw = new popupwindow(findviewbyid(r.id.popup), 100, 100, true); pw.showatlocation(findviewbyid(r.id.main), gravity.center, 0, 0);
second did exact same code:
final layoutinflater inflater = (layoutinflater)this.getsystemservice(context.layout_inflater_service); popupwindow pw = new popupwindow(inflater.inflate(r.layout.popup, (viewgroup) findviewbyid(r.layout.main) ), 100, 100, true); pw.showatlocation(findviewbyid(r.id.main_page_layout), gravity.center, 0, 0);
the first throws nullpointerexception , second throws badtokenexception , says "unable add window -- token null not valid"
what in world doing wrong? i'm extremely novice please bear me.
to avoid badtokenexception, need defer showing popup until after lifecycle methods called (-> activity window displayed):
findviewbyid(r.id.main_page_layout).post(new runnable() { public void run() { pw.showatlocation(findviewbyid(r.id.main_page_layout), gravity.center, 0, 0); } });
Comments
Post a Comment