Android badtokenexception Problem Solving

Source: Internet
Author: User

The preface today found the "android.view.windowmanager$badtokenexception" problem in the test process, here to record the solution. (PS: The first application on-line, feel bug or more, feel because of this application, can be a trial period is a problem, can only insist on refueling).
Problem analysis This kind of problem in fact in the error log can give a good hint, the following is a partial list of useful error logs, as follows:
Through the log, it can be known that this problem is generally due to the asynctask in the OnPostExecute to perform a startup dialog operation, but the current activity has been destroyed, write an error code to reproduce the problem:
Package Com.example.testlibrary;import Android.app.activity;import Android.app.alertdialog;import Android.content.dialoginterface;import Android.content.dialoginterface.onclicklistener;import Android.os.asynctask;import Android.os.bundle;public class Mainactivity extends Activity {private Updateuiasynctask Asynctask; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); updateuiprocess ();} private void Updateuiprocess () {asynctask = new updateuiasynctask (); Asynctask.execute ();} Private class Updateuiasynctask extends Asynctask<void, void, boolean> {@Overrideprotected void OnPostExecute ( Boolean result) {if (!iscancelled () && result) {Alertdialog.builder Builder = new Alertdialog.builder ( mainactivity.this). Settitle ("title"). Setmessage ("This is the content"). Setpositivebutton ("OK", new Onclicklistener () {@ overridepublic void OnClick (dialoginterface dialog, int which) {Dialog.dismiss ();}}). Setnegativebutton ("Cancel", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {Dialog.dismiss ();}}); Alertdialog dialog = Builder.create ();d ialog.show ();}} @Overrideprotected Boolean doinbackground (Void ... params) {if (!iscancelled ()) {try {thread.sleep (6000);} catch ( Interruptedexception e) {return false;} return true;} return true;}}}
Launch the application and exit the application within 6s and return to build, and you will see an exception to the application:
Solution if the current activity has been destroyed (either by calling finish () or by pressing the back, home key), but the Asynctask asynchronous task in the activity does not end, And a call to Alertdialog.show () in OnPostExecute will appear:
Android.view.windowmanager$badtokenexception:unable to add window-token [email protected] was not valid; Is your activity running?.
The workaround is also simple: Before Alertdialog.show () executes, to determine whether the current activity has been destroyed, you can use the Isfinishing () method provided by Android. At the same time, you should also increase the cleanup asynctask operation in the activity, the sample code is as follows:
Package Com.example.testlibrary;import Android.app.activity;import Android.app.alertdialog;import Android.content.dialoginterface;import Android.content.dialoginterface.onclicklistener;import Android.os.asynctask;import Android.os.bundle;public class Mainactivity extends Activity {private Updateuiasynctask Asynctask; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); updateuiprocess ();} @Overrideprotected void OnDestroy () {//Increase cleanup aysnctask action if (asynctask! = null && asynctask.getstatus ()! = Asynctas k.status.finished) {Asynctask.cancel (true);} Super.ondestroy ();} private void Updateuiprocess () {asynctask = new updateuiasynctask (); Asynctask.execute ();} Private class Updateuiasynctask extends Asynctask<void, void, boolean> {@Overrideprotected void OnPostExecute ( Boolean result) {//Show dialog before adding to determine whether the current activity is the finish operation if (!iscancelled () && result && Mainactivity.this! = null &&! MainActivity.this.isFinishing ()) {Alertdialog.builder Builder = new Alertdialog.builder (mainactivity.this). Settitle ("title"). Setmessage ("This is the content"). Setpositivebutton ("OK", new Onclicklistener () {@Overridepublic void OnClick ( Dialoginterface dialog, int which) {Dialog.dismiss ();}}). Setnegativebutton ("Cancel", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) { Dialog.dismiss ();}}); Alertdialog dialog = Builder.create ();d ialog.show ();}} @Overrideprotected Boolean doinbackground (Void ... params) {if (!iscancelled ()) {try {thread.sleep (6000);} catch ( Interruptedexception e) {return false;} return true;} return true;}}}




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.