如果在activity中声明了PopupWindow,确保在关闭当前activity时候,PopupWindow已经关闭,否则会引起Window Leaked。异常如下:
E/WindowManager: android.view.WindowLeaked: Activity com.xxx.XXXActivity has leaked window android.widget.PopupWindow$PopupDecorView{8b0d12b V.E...... R....... 0,0-55,66} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:733)
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:717)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:399)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:109)
at android.widget.PopupWindow.invokePopup(PopupWindow.java:1576)
at android.widget.PopupWindow.showAtLocation(PopupWindow.java:1342)
at android.widget.PopupWindow.showAtLocation(PopupWindow.java:1308)
at org.chromium.android_webview.PopupTouchHandleDrawable.show(chromium-TrichromeWebViewGoogle.apk-stable-410410681:18)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:335)
at android.os.Looper.loop(Looper.java:183)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
原因 Android的每一个Activity都有个WindowManager窗体管理器,构建在某个Activity之上的对话框,PopupWindow也有相应的WindowManager窗体管理器。因为对话框、PopupWindown不能脱离Activity而单独存在着,所以当某个PopupWindow正在显示的时候我们去finish()了承载该PopupWindow的Activity时,就会抛Window Leaked异常了,因为这个PopupWindow的WindowManager已经没有谁可以附属了,所以它的窗体管理器已经泄漏了。
解决方法 关闭(finish)某个Activity前,要确保附属在上面的PopupWindow已经关闭(dismiss)了。如下:
if (popWindow!= null) {
popWindow.dismiss();
}
|