在使用dialog或者popupwindow的时候studio出现如下异常
06-01 17:27:05.369 25662-25662/com.dearxy.malldear E/WindowManager: android.view.WindowLeaked: Activity com.dearxy.malldear.ui.details.view.DetailsActivity has leaked window android.widget.PopupWindow$PopupDecorView{b212323 V.E...... ........ 0,0-1440,353} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:565)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:326)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.widget.PopupWindow.invokePopup(PopupWindow.java:1329)
at android.widget.PopupWindow.showAtLocation(PopupWindow.java:1077)
at android.widget.PopupWindow.showAtLocation(PopupWindow.java:1035)
at com.dearxy.malldear.ui.details.view.DetailsActivity.openSharePlatform(DetailsActivity.java:252)
at com.dearxy.malldear.ui.details.view.DetailsActivity.onViewClicked(DetailsActivity.java:201)
at com.dearxy.malldear.ui.details.view.DetailsActivity_ViewBinding$2.doClick(DetailsActivity_ViewBinding.java:61)
at butterknife.internal.DebouncingOnClickListener.onClick(DebouncingOnClickListener.java:22)
at android.view.View.performClick(View.java:5697)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7237)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
原因分析:
Android的每一个Activity都有个WindowManager窗体管理器,同样,构建在某个Activity之上的对话框、PopupWindow也有相应的WindowManager窗体管理器。因为对话框、PopupWindown不能脱离Activity而单独存在着,所以当某个Dialog或者某个PopupWindow正在显示的时候我们去finish()了承载该Dialog(或PopupWindow)的Activity时,就会抛Window Leaked异常了,因为这个Dialog(或PopupWindow)的WindowManager已经没有谁可以附属了,所以它的窗体管理器已经泄漏了。
解决办法:
关闭(finish)某个Activity前,要确保附属在上面的Dialog或PopupWindow已经关闭(dismiss)了。