Today, when practicing the Popupwindow popup, I intend to show the popup box and display it under the specified view when the interface is loaded.
The preliminary method is to directly execute the Showpopupwindows method directly inside the Onresume method.
But the quote "Unable to add window--token null was not valid; Is your activity running? "
Reason reference: http://cb269267.iteye.com/blog/1787779
The reasons for this are as follows: Popupwindow must specify a view, and if the view has not been created, the error will be reported.
The solution is to delay the execution of the Showpopupwindows method for some time. The link above is the idea.
Method One: Direct use of handle delay
Public void Onresume () { super. Onresume (); // mode one: direct use of handle delay Message msg = pophandler.obtainmessage (); = 0; = 0; = Toptab_items.get (0); );}
PrivateHandler Pophandler =NewHandler () {@Override Public voidhandlemessage (Message msg) {Switch(msg.what) { Case0: View View=(View) msg.obj; intindex =Msg.arg1; Showpopupwindows (View,index); Pophandler.removecallbacks (runnable); Break; default: Break; } } };
Method Two: Delay through the child thread
Public void Onresume () { super. Onresume (); // mode two: Child thread delay Pophandler.post (runnable); }
PrivateHandler Pophandler =NewHandler () {@Override Public voidhandlemessage (Message msg) {Switch(msg.what) { Case0: View View=(View) msg.obj; intindex =Msg.arg1; Showpopupwindows (View,index); if(Timer! =NULL) {timer.cancel (); } pophandler.removecallbacks (runnable); Break; default: Break; } } };
PrivateTimer timer; //Create a child thread PrivateRunnable Runnable =NewRunnable () { Public voidrun () {Try{Timer=NewTimer (); TimerTask Task=NewTimerTask () { Public voidrun () {Message msg=Pophandler.obtainmessage (); Msg.what= 0; Msg.arg1= 0; Msg.obj= Toptab_items.get (0); Pophandler.sendmessage (msg); } }; Timer.schedule (Task,500); } Catch(Exception e) {//Todo:handle Exception } } };
Note:
/** Display Popupwindow Popup under the specified view */
private void Showpopupwindows (view view, int index) {.... }
Practice the implementation of the Popupwindow popup box when loading the display popup to the specified view below--two methods of delay