Android:WindowLeaked在旋转屏幕和对话框打开时

时间:2012-08-23 14:23:16

标签: android dialog

当您点击我的活动中的图片时,我会显示一个对话框。当对话框打开并且用户旋转屏幕时,我的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消息的原因。

有什么想法吗?我的变量声明/实例化一定是错的。感谢。

2 个答案:

答案 0 :(得分:0)

没关系,我明白了。问题是,我有两个图像都使用相同的对话框变量(触摸时实例化它并启动它们自己的对话框)。因此,第二个图像通过将其再次声明为final Dialog dialog ...来引用对话框。在我将dialog作为全局变量之前,我已经这样了。

删除重新声明后,一切都很完美。

顺便说一下,对于两个不同的对话框使用相同的对话框变量是否有任何风险,这些对话框只能在不同的时间启动?

答案 1 :(得分:0)

如果您将屏幕更改为纵向或纵向横向,则在重绘视图之前重绘您的视图需要关闭对话框

所以, 尝试将此属性android:configChanges="orientation"添加到AndroidManifest.xml文件中的Activity元素。