Analysis of Java. Lang. illegalargumentexception: view not attached to Window Manager errors

Source: Internet
Author: User

Today, I encountered a very strange problem. When the user sets the PIN code and unlocks the PIN code normally on the screen lock page, it is displayed that com. Android. Phone has stopped running when you enter launcher. At first, I wondered if the phone process reported an error because it was handled when I unlocked the PIN code. I found the possible cause through log analysis:

AndroidRuntime: FATAL EXCEPTION: mainAndroidRuntime: java.lang.IllegalArgumentException: View not attached to window managerAndroidRuntime: at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:385)AndroidRuntime: at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:287)AndroidRuntime: at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:79)AndroidRuntime: at android.app.Dialog.dismissDialog(Dialog.java:323)AndroidRuntime: at android.app.Dialog.dismiss(Dialog.java:306)AndroidRuntime: at com.android.stk.StkDialogActivity$4.onClick(StkDialogActivity.java:188)AndroidRuntime: at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:169)AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)AndroidRuntime: at android.os.Looper.loop(Looper.java:153)AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5299)AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)


Reprint please be sure to indicate the source: http://blog.csdn.net/yihongyuelanBy analyzing the unlock process of the PIN code, we know that it is not a problem caused by the dialog during the PIN code interpretation. The pin code unlocking process is described in other documents. After carefully checking the log, it is found that:
AndroidRuntime: at com.android.stk.StkDialogActivity$4.onClick(StkDialogActivity.java:188)

This is the pop-up STK dialog box after the SIM card is recognized at startup. Why does the message "com. Android. Phone" fail to run?

After the above analysis, we can roughly guess that the problem is caused by STK. Since it is a STK problem, why does it report a phone error? If it is a phone error, you can retrieve phone-related questions from the log. Why not? Then we will answer these questions one by one.

(1) Why is the com. Android. Phone running stopped?

View the STK source code (the Single-card MTK code is in/mediatec/packages/APP/stk1) and in its androidmanifest. XML: Android: shareduserid = "android. UID. phone ", that is, STK and Phon, is in the same process. Therefore, in terms of error reporting, it will be com. andorid. phone. (2) Why is the view not attached to Window Manager error reported?

This error means that the view we operate is not included in the management of Window Manager. We know that all window creation and management are attached to window manager, so the creation of dialog is no exception. The creation process of dialog can be seen by viewing the source code. In the dialog constructor, a window object is created, but we know that the window object is not used for display, the view object is actually used for display. Therefore, a view object of mdecor is constructed using the show method of dialog, And the dialog is displayed using the addview () method of windowmanager. By viewing the log information, we can see COM. android. STK. stkdialogactivity $4. onclick (stkdialogactivity. java: 188) Check the corresponding stkdialogactivity code and find that the code in row 188 is dialog. dismiss (); after searching on the network, it is found that this error occurs in most cases. It is caused by the existence of the activity that creates the dialog when dismiss dialog is found. For example, if a dialog is displayed on the interface, dismiss the dialog after the task processing is complete. If the activity is killed and restarted for some reason during the dialog display, windowmanager checks when the task ends and dismiss dialog, you will find that the activity to which the dialog belongs does not exist (restart once, it is a new activity), so the illegalargumentexception will be reported:
View not attached to window manager. through the above analysis, we can know that when STK dialog executes the dismiss method, it is found that the activity to start it is gone and killed (now it is restarted ), therefore, an error is reported. (3) Why is stkdialogactivity "killed "?

By tracking and viewing the exception log, we can see that the life cycle print log of stkdialogactivity is as follows:
Oncreateonresume-mbsendresp [false], Sim ID: 0 ...... omit some onsaveinstancestateonpause, Sim ID: 0 ...... omit part of ondestroy-oncreate ...... omit part of onrestoreinstancestate-[COM. android. internal. telephony. cat. textmessage @ 41fe7d80] onresume-mbsendresp [false], Sim ID: 0
An exception occurs after this error ...... Log shows that the activity was started twice. In the log, we also see the onsaveinstancestate method. The onsaveinstancestate method is described in the android SDK as follows: Android callonsaveinstancestate () before the activity becomes vulnerable to being destroyed by the system, but does not bother calling it when the instance is actually being destroyed by a user action
(Such as pressing the back key) that when an activity becomes "easy" to be destroyed by the system, the onsaveinstancestate of the activity will be executed, unless the activity is manually destroyed by the user, for example, when the user presses the back key. Note that the current activity is not destroyed, but the system thinks it may be destroyed, so this method will be executed. In this method, we can save various data information in the activity. If the activity is actually killed and restarted, we can use the onrestoreinstancestate method to restart the activity, restore the previously saved data. The call of the onsaveinstancestate method follows an important principle: when the system "destroys your activity without your permission", the onsaveinstancestate will be called by the system. This is the responsibility of the system, because it must provide an opportunity for you to save your data (of course, if you do not save it yourself, you cannot recover it ). Therefore, through the above analysis, we can see that stkdialogactivity is started again after it is killed, but why is it killed? No answer is found through log. (4). Does dialog exist when stkdialogactivity is killed? There may be some questions. If the dialog is not displayed, an error occurs. How can I ensure that the dialog is displayed at that time? We can continue searching in the log and find that:
WindowManager: Activity com.android.stk.StkDialogActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41fea228 V.E..... R.....ID 0,0-1026,433} that was originally added hereWindowManager: android.view.WindowLeaked: Activity com.android.stk.StkDialogActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41fea228 V.E..... R.....ID 0,0-1026,433} that was originally added hereWindowManager: at android.view.ViewRootImpl.<init>(ViewRootImpl.java:409)WindowManager: at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:218)WindowManager: at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)WindowManager: at android.app.Dialog.show(Dialog.java:281)WindowManager: at android.app.AlertDialog$Builder.show(AlertDialog.java:951)WindowManager: at com.android.stk.StkDialogActivity.onCreate(StkDialogActivity.java:192)WindowManager: at android.app.Activity.performCreate(Activity.java:5122)WindowManager: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)WindowManager: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)WindowManager: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)WindowManager: at android.app.ActivityThread.access$600(ActivityThread.java:156)WindowManager: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)WindowManager: at android.os.Handler.dispatchMessage(Handler.java:99)WindowManager: at android.os.Looper.loop(Looper.java:153)WindowManager: at android.app.ActivityThread.main(ActivityThread.java:5299)WindowManager: at java.lang.reflect.Method.invokeNative(Native Method)WindowManager: at java.lang.reflect.Method.invoke(Method.java:511)WindowManager: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)WindowManager: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)WindowManager: at dalvik.system.NativeStart.main(Native Method)

What does windowmanager's error mean? This log is an error thrown by windowmanager. If our dialog has not been dismiss, if the activity is destroyed, the above error will occur, prompting the window leakage (leaked
Window ). Here, I also made an experiment and wrote a demo to display a dialog in the oncreate method of the activity, and then directly called the finish method. The code is roughly as follows:

    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        AlertDialog.Builder info = new Builder(this);        info.setTitle("Dialog").setPositiveButton("OK", null).setMessage("This is a Dialog");;        info.show();         finish();    }

Run the program directly to the simulator or on the real machine. view the log to see the leaked window error message. Therefore, this proves that the dialog attached to stkactivity exists when it is destroyed.

(5) How can this problem be solved?

After the above analysis, we know the cause of the problem. How can we solve it? You can solve the problem through the following two aspects: 1. the dialog control method provided by activity requires a dialog box in the activity. You can use the callback method provided by activity, such as oncreatedialog (), showdialog (), dimissdialog (), and removedialog. After all, these are built-in methods of the activity, so it is more convenient to use, and you do not need to display the creation and control of the dialog object. Everything is controlled by the framework, which is relatively safer. 2. restrict the lifecycle of the dialog object so that the lifecycle of the created dialog object is consistent with that of the activity. That is to say, the lifecycle of the dialog object is limited between the oncreate () and ondestroy () Methods of the activity.

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.