Practice the implementation of the Popupwindow popup box when loading the display popup to the specified view below--two methods of delay

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.