Are you sure you want to use Android dialog? Thought of by an exception

Source: Internet
Author: User

A bug
The previous few days showed that such a bug was a runtimeexception. The details are as follows:
Java. Lang. illegalargumentexception: view not attached to Window Manager
At Android. View. windowmanagerimpl. findviewlocked (windowmanagerimpl. Java: 356)
At Android. View. windowmanagerimpl. removeview (windowmanagerimpl. Java: 201)
At Android. View. Window $ localwindowmanager. removeview (window. Java: 400)
At Android. App. Dialog. dismissdialog (dialog. Java: 268)
At Android. App. Dialog. Access $000 (dialog. Java: 69)
At Android. App. Dialog $ 1.run( dialog. Java: 103)
At Android. App. Dialog. Dismiss (dialog. Java: 252)
At XXX. onpostexecute (xxx $1. Java: XXX)
First, Google found that the cause is basically the same when the dismiss dialog box, the activity no longer exists. Normally, this type of exception occurs when there is a time-consuming thread operation. You need to display a progressdialog and a dialog box at the beginning of the task. Then, when the task is completed, the dismiss dialog box is displayed, if the activity is killed for some reason and restarted during this period, when dismiss occurs, windowmanager checks and finds that the activity to which dialog belongs does not exist, so illegalargumentexception is reported: view not attached to window manager.
In fact, an important reason for such exceptions is that the creation, display, and cancellation of progressdialog allow the process in a non-UI thread. In Android, non-UI threads do not allow operations on the UI, such as adding and removing views. But why can I create a display or cancel dialog box? This exception may also cause the application crash.
To avoid such exceptions, you must correctly use the dialog box and use the thread correctly.
Correct use dialog box
Do not create, display, or cancel a dialog box in a non-UI thread.
What should I do if the dialog box is displayed for asynchronous operations? Activities have callbacks for corresponding operation dialogs, such as oncreatedialog (), showdialog (), dimissdialog (), and removedialog. These are all activity methods, so they are more convenient to use and do not need to show how to create and manipulate the dialog object. Everything is controlled by the framework, which is relatively safer.
In addition, make sure that the dialog box object is within the controllable range and lifecycle of the activity. For example, it must be a member variable and make the dialog box variable active in oncreate () of the activity () and ondestroy.
Use the correct thread www.2cto.com
Use as few independent threads as possible. Do not use Java-style anonymous threads unless it is a real time-consuming operation, after the operation is completed, you do not need to do anything else.
Use classes provided by android as much as possible, such as asynctask. In addition, if the thread operation needs to interact with the main thread, it is best to save the object of a thread and have some control in the thread, so that the activity can better control the thread.
If some operations are time-consuming and regular operations, such as getting data from the network, reading files from the background, or importing/exporting, and restoring/backing up data, it is best to put it in the background service and give the corresponding progress in statusbar.

Related Article

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.