关于android.view.WindowLeaked(窗体泄露)的解决方案

07-09 18:07:07.708: E/WindowManager(4843): android.view.WindowLeaked: Activity com.koridyphone.weikeclass.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{304597a0 V.E..... R.....I. 0,0-1026,499} that was originally added here
07-09 18:07:07.708: E/WindowManager(4843):     at android.view.ViewRootImpl.(ViewRootImpl.java:457)
07-09 18:07:07.708: E/WindowManager(4843):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:278)
07-09 18:07:07.708: E/WindowManager(4843):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
07-09 18:07:07.708: E/WindowManager(4843):     at android.app.Dialog.show(Dialog.java:311)
07-09 18:07:07.708: E/WindowManager(4843):     at com.koridyphone.weikeclass.view.PerSetDialog.show(PerSetDialog.java:72)
07-09 18:07:07.708: E/WindowManager(4843):     at com.koridyphone.weikeclass.view.PerSetDialog.show(PerSetDialog.java:82)
07-09 18:07:07.708: E/WindowManager(4843):     at com.koridyphone.weikeclass.unitl.Per_Info.showNoLoginDialog(Per_Info.java:79)
07-09 18:07:07.708: E/WindowManager(4843):     at com.koridyphone.weikeclass.MainActivity.getPerInfo(MainActivity.java:210)
07-09 18:07:07.708: E/WindowManager(4843):     at com.koridyphone.weikeclass.MainActivity.initView(MainActivity.java:112)
07-09 18:07:07.708: E/WindowManager(4843):     at com.koridyphone.weikeclass.base.BaseActivity.onCreate(BaseActivity.java:22)
07-09 18:07:07.708: E/WindowManager(4843):     at android.app.Activity.performCreate(Activity.java:6122)
07-09 18:07:07.708: E/WindowManager(4843):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1114)
07-09 18:07:07.708: E/WindowManager(4843):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2481)
07-09 18:07:07.708: E/WindowManager(4843):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2614)
07-09 18:07:07.708: E/WindowManager(4843):     at android.app.ActivityThread.access$800(ActivityThread.java:178)
07-09 18:07:07.708: E/WindowManager(4843):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
07-09 18:07:07.708: E/WindowManager(4843):     at android.os.Handler.dispatchMessage(Handler.java:111)
07-09 18:07:07.708: E/WindowManager(4843):     at android.os.Looper.loop(Looper.java:194)
07-09 18:07:07.708: E/WindowManager(4843):     at android.app.ActivityThread.main(ActivityThread.java:5643)
07-09 18:07:07.708: E/WindowManager(4843):     at java.lang.reflect.Method.invoke(Native Method)
07-09 18:07:07.708: E/WindowManager(4843):     at java.lang.reflect.Method.invoke(Method.java:372)
07-09 18:07:07.708: E/WindowManager(4843):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)

07-09 18:07:07.708: E/WindowManager(4843):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

按字面了解,Window Leaked大概就是说一个窗体泄漏了,也就是我们常说的内存泄漏,为什么窗体会泄漏呢?

产生原因:
我们知道Android的每一个Activity都有个WindowManager窗体管理器,同样,构建在某个Activity之上的对话框、PopupWindow也有相应的WindowManager窗体管理器。因为对话框、PopupWindown不能脱离Activity而单独存在着,所以当某个Dialog或者某个PopupWindow正在显示的时候我们去finish()了承载该Dialog(或PopupWindow)的Activity时,就会抛Window Leaked异常了,因为这个Dialog(或PopupWindow)的WindowManager已经没有谁可以附属了,所以它的窗体管理器已经泄漏了。

android.view.WindowLeaked一般会发生在Activity 与Dialog的显示。

       Activity 中create 一个Dialog,若你先关闭Dialog再关闭Activity就是正常的,若你先关闭Activity再关闭Dialog就会报错这个android.view.WindowLeaked错误了。
       分析这个原因是:Dialog是基于Activity而创建的:new ProgressDialog(this);this 就是Activity。 Activtity先finish,那Dialog就没得依附了,所以就会报android.view.WindowLeaked。



解决方法:
关闭(finish)某个Activity前,要确保附属在上面的Dialog或PopupWindow已经关闭(dismiss)了。
   也可在OnCreateDialog()中创建Dialog,让系统去管理对话框

你可能感兴趣的:(关于android.view.WindowLeaked(窗体泄露)的解决方案)