【Crash经验总结】not attached to window manager

【问题描述】

如下报错

"Fatal Exception: java.lang.IllegalArgumentException: View=DecorView@66e8333[xxxActivity] not attached to window manager
       at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:511)
       at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:413)
       at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:168)
       at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4439)
       at android.app.ActivityThread.-wrap6(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1629)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:156)
       at android.app.ActivityThread.main(ActivityThread.java:6524)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)"

 

【问题原因】

在移除view的时候,没有检查其activity的可用性,导致上述报错

 

【解决方案】

public static boolean isUseable(Activity mActivity) {
    if ((null == mActivity) || mActivity.isFinishing() || mActivity.isRestricted()) {
        return false;
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        try {
            if (mActivity.isDestroyed()) {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } catch (Error e) {
            e.printStackTrace();
        }
    }

    return true;
}

 

你可能感兴趣的:(安卓,【Crash经验总结】)