問題的解決方法很簡單,這裡主要是介紹一個除錯小技巧,且聽事件由來:
場景:Activity 中彈出一個 Dialog,幾秒後會 dismiss。平時測試沒遇到崩潰,但是崩潰平臺卻捕捉到如下錯誤:
java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{41ef97f8 V.E..... R.....ID 0,0-480,762} not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:370)
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:299)
at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:84)
at android.app.Dialog.dismissDialog(Dialog.java:329)
at android.app.Dialog.dismiss(Dialog.java:312)
at com.quickdy.vpn.dialog.b.dismiss(ConnectingDialog.java:229)
at com.quickdy.vpn.dialog.b.i(ConnectingDialog.java:436)
at com.quickdy.vpn.dialog.b.j(ConnectingDialog.java:59)
at com.quickdy.vpn.dialog.b$a.onFinish(ConnectingDialog.java:262)
at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
複製程式碼
原因就是 Dialog dismiss 的時候, Activity 的視窗已經被銷燬。 stackoverflow 上有人提了這個奇技淫巧:
1. Enable this option on your device: Settings -> Developer Options - Don't keep Activities.
2. Press Home button while the AsyncTask is executing and the ProgressDialog is showing.
這樣就能模擬“對話方塊展示過程中,Activity 被銷燬的情況”。
另外,附上該問題解決辦法:在 Activity / Fragment 的 onDestroy 方法,加上如下程式碼:
@Override
protected void onDestroy() {
if (dialog != null) {
dialog.dismiss();
}
super.onDestroy();
}
複製程式碼