我有一个应用程序在重启时崩溃,因为它会立即显示一个对话框。这只发生在我重启设备时(应用程序有一个Boot接收器),否则,只需自己启动应用程序,它就可以正常工作。该对话框由主活动调用。这是代码:
public class AssistekAlertDialog extends Dialog {
@Override
public void onAttachedToWindow() {
getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
///Exception^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
KeyguardManager manager = (KeyguardManager)con.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock keylock = manager.newKeyguardLock(Context.KEYGUARD_SERVICE);
keylock.disableKeyguard();
super.onAttachedToWindow();
}
}
这是logcat:
03-05 15:38:08.292: E/AndroidRuntime(1505): FATAL EXCEPTION: main
03-05 15:38:08.292: E/AndroidRuntime(1505): java.lang.IllegalArgumentException: View not attached to window manager
03-05 15:38:08.292: E/AndroidRuntime(1505): at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:383)
03-05 15:38:08.292: E/AndroidRuntime(1505): at android.view.WindowManagerGlobal.updateViewLayout(WindowManagerGlobal.java:272)
03-05 15:38:08.292: E/AndroidRuntime(1505): at android.view.WindowManagerImpl.updateViewLayout(WindowManagerImpl.java:74)
03-05 15:38:08.292: E/AndroidRuntime(1505): at android.app.Dialog.onWindowAttributesChanged(Dialog.java:677)
03-05 15:38:08.292: E/AndroidRuntime(1505): at android.view.Window.setType(Window.java:637)
03-05 15:38:08.292: E/AndroidRuntime(1505): at com.assistek.ediary.AssistekAlertDialog.onAttachedToWindow(AssistekAlertDialog.java:298)
我可以通过使用try-catch块来摆脱异常,但这似乎很懒惰。有没有其他方法来处理此异常?