当您单击我活动中的图像时,我会弹出一个对话框。当对话框打开并且用户旋转屏幕时,我的 LogCat 中显示以下内容:
08-23 09:47:47.151: E/WindowManager(15589): Activity my.activity.MyActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@43554748 that was originally added here
08-23 09:47:47.151: E/WindowManager(15589): android.view.WindowLeaked: Activity my.activity.MyActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@43554748 that was originally added here
08-23 09:47:47.151: E/WindowManager(15589): at android.view.ViewRoot.<init>(ViewRoot.java:247)
08-23 09:47:47.151: E/WindowManager(15589): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
08-23 09:47:47.151: E/WindowManager(15589): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-23 09:47:47.151: E/WindowManager(15589): at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-23 09:47:47.151: E/WindowManager(15589): at android.app.Dialog.show(Dialog.java:241)
08-23 09:47:47.151: E/WindowManager(15589): at tip.machine.MyActivity$8.onClick(MyActivity.java:385)
08-23 09:47:47.151: E/WindowManager(15589): at android.view.View.performClick(View.java:2408)
08-23 09:47:47.151: E/WindowManager(15589): at android.view.View$PerformClick.run(View.java:8816)
08-23 09:47:47.151: E/WindowManager(15589): at android.os.Handler.handleCallback(Handler.java:587)
08-23 09:47:47.151: E/WindowManager(15589): at android.os.Handler.dispatchMessage(Handler.java:92)
08-23 09:47:47.151: E/WindowManager(15589): at android.os.Looper.loop(Looper.java:123)
08-23 09:47:47.151: E/WindowManager(15589): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-23 09:47:47.151: E/WindowManager(15589): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 09:47:47.151: E/WindowManager(15589): at java.lang.reflect.Method.invoke(Method.java:521)
08-23 09:47:47.151: E/WindowManager(15589): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
08-23 09:47:47.151: E/WindowManager(15589): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-23 09:47:47.151: E/WindowManager(15589): at dalvik.system.NativeStart.main(Native Method)
我所做的是通过编写全局声明对话框Dialog dialog
,在我的 中onPause
,我写了这个:
@Override
protected void onPause() {
System.out.println("IN PAUSE!");
try {
dialog.dismiss();
System.out.println("IN TRY!");
}
catch (java.lang.NullPointerException exception) {System.out.println("IN CATCH!");}
super.onPause();
}
我想,如果dialog
没有实例化,catch 块会捕获错误。无论如何,每次我旋转时,无论对话框是否打开,我都只进入 catch 块。当对话框打开并旋转时,我得到与上面相同的堆栈跟踪(它直接进入 catch 块)。因此,它似乎从未尝试过dialog.dismiss()
,这就是为什么我仍然收到WindowLeaked
消息的原因。
有任何想法吗?我的变量声明/实例化一定有问题。谢谢。