1

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

有任何想法吗?我的变量声明/实例化一定有问题。谢谢。

4

2 回答 2

0

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

去掉重新声明后,一切都完美了。

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

于 2012-08-23T14:27:58.163 回答
-1

如果您是屏幕将横向更改为纵向或纵向更改为横向,则此原因在重绘视图之前重绘您的视图,您必须关闭您的对话框

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

于 2013-12-13T20:38:54.310 回答