关于dialog出现not attached to window manager问题深入分析

java.lang.RuntimeException: Unable to destroy activity {com.mexuewang.mexueteacher/com.mexuewang.mexueteacher.main.MainActivity}: java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{42557ff8 V.E..... R......D 0,0-342,343} not attached to window manager
	at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3499)
	at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3517)
	at android.app.ActivityThread.access$1400(ActivityThread.java:141)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
	at android.os.Handler.dispatchMessage(Handler.java:102)
	at android.os.Looper.loop(Looper.java:136)
	at android.app.ActivityThread.main(ActivityThread.java:5113)
	at java.lang.reflect.Method.invokeNative(Native Method)
	at java.lang.reflect.Method.invoke(Method.java:515)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
	at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{42557ff8 V.E..... R......D 0,0-342,343} not attached to window manager
	at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:377)
	at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:306)
	at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:84)
	at android.app.Dialog.dismissDialog(Dialog.java:329)
	at android.app.Dialog.dismiss(Dialog.java:312)
	at com.mexuewang.mexueteacher.util.y.a(ShowDialog.java:40)
	at com.mexuewang.mexueteacher.main.b.onDestroy(GrowUpOfTeacher.java:711)
	at android.support.v4.app.Fragment.performDestroy(Fragment.java:1698)
	at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1056)
	at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
	at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
	at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1914)
	at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:329)
	at com.mexuewang.mexueteacher.main.MainActivity.onDestroy(MainActivity.java:448)
	at android.app.Activity.performDestroy(Activity.java:5420)

at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1117

我们报了一个这样的问题:这个问题分析如下:

原理:所有的窗口创建都得通过windowManager.dialog在创建时,利用activity的window对象,window对象中还有一个mDecorView对象,dialog在调用show的时候调用addview方法,将dialog显示出来,如果我们在一个界面显示一个dialog,在dialog运行期间,由于activity被系统回收或又重新启时, dialog在调用dismiss方法时会进行windowmanager检查,发现dialog所在的acitivty不再存在了。所以会报illegalArgumentException:View not attached to window manager.如果activity关掉的时候ondestroy中没有停止dialog那么会出现泄露的情况。     解决办法:     1. 使用Activity自带的Dialog       比如onCreateDialog(),showDialog(),dimissDialog(),removeDialog()等,这样一切都由activity操控,相对来说比较安全。 2. 限制Dialog的生命周期     让创建的Dialog对象的存活周期跟Activity的生命周期一致,也就是说Dialog的生命周期被限定在Activity的onCreate()和onDestroy()方法之间。

你可能感兴趣的:(关于dialog出现not attached to window manager问题深入分析)