Android 报错处理:not attached to window manager

not attached to window manager有许多场景发生

最近在dialog上碰到此问题

WindowManager: android.view.WindowLeaked: Activity com.XXXXXX.LoginActivity has leaked window DecorView@613753a[系统信息] that was originally added here
12407-17 16:15:41.822 30086 30086 E WindowManager: at android.view.ViewRootImpl.(ViewRootImpl.java:498)
12507-17 16:15:41.822 30086 30086 E WindowManager: at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:346)
12607-17 16:15:41.822 30086 30086 E WindowManager: at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94)
12707-17 16:15:41.822 30086 30086 E WindowManager: at android.app.Dialog.show(Dialog.java:356)
12807-17 16:15:41.822 30086 30086 E WindowManager: at com.growingio.android.sdk.agent.VdsAgent.showDialog(Unknown Source:0)
12907-17 16:15:41.822 30086 30086 E WindowManager: at com.growingio.android.sdk.agent.VdsAgent.showDialog(Unknown Source:0)
13007-17 16:15:41.822 30086 30086 E WindowManager: at com.growingio.android.sdk.agent.VdsAgent.showDialog(Unknown Source:0)

可能原因:

之前显示的对话框没有关闭,或者上下文已经失效,存在窗口泄露问题

解决方法:

1、try catch

try {
    if ((mDialog != null) && mDialog.isShowing()) {
      mDialog.dismiss();
    }
} catch (Exception e) {
       
} finally {
    mDialog = null;
}  

2、判断上下文,有效时再关闭

if (progressDialog != null) {
       if (progressDialog.isShowing()){
           Context context = ((ContextWrapper)progressDialog.getContext()).getBaseContext();
       if(context instanceof Activity) {
           if(!((Activity)context).isFinishing() && !((Activity)context).isDestroyed())
                  progressDialog.dismiss();
           } else {
                  progressDialog.dismiss();
           }
        }
       progressDialog = null;
}

 

你可能感兴趣的:(Android,报错处理)