android.view.WindowLeaked的解决方案

无意中发现windowleaked,错误详细信息如下:

11-28 16:39:10.867: E/WindowManager(14064): android.view.WindowLeaked: Activity com.sbdz.agriculture.activity.ProductActivity has leaked window android.widget.TextView{45ebeb60 I.ED.... ......I. 0,0-210,348} that was originally added here

11-28 16:39:10.867: E/WindowManager(14064):     at android.view.ViewRootImpl.(ViewRootImpl.java:389)
11-28 16:39:10.867: E/WindowManager(14064):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
11-28 16:39:10.867: E/WindowManager(14064):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
11-28 16:39:10.867: E/WindowManager(14064):     at com.sbdz.agriculture.activity.SideBar.init(SideBar.java:66)
11-28 16:39:10.867: E/WindowManager(14064):     at com.sbdz.agriculture.activity.SideBar.(SideBar.java:47)

翻阅资料,发现产生原因:

Android的每一个Activity都有个WindowManager窗体管理器,同样,构建在某个Activity之上的Dialog、PopupWindow也有相应的WindowManager窗体管理器。Dialog、PopupWindown不能脱离Activity而单独存在着,所以当某个Dialog或者某个PopupWindow正在显示的时候我们去finish()了承载该Dialog(或PopupWindow)的Activity时,会抛Window Leaked异常了。


具体解决办法:退出Dialog、PopupWindow之前,先调用dismiss();



你可能感兴趣的:(android开发)