android - View 未附加到窗口管理器,对话框关闭

标签 android

所以我有一个名为 GameActivity.java 的 Activity ,在这个 Activity 中我调用了 DialogAnswer.show(),它简单地在屏幕上显示了一些图片。

java.lang.IllegalArgumentException: View not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:402)
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:304)
at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:79)
at android.app.Dialog.dismissDialog(Dialog.java:325)
at android.app.Dialog.dismiss(Dialog.java:307)
at pl.evelanblog.prawdaczyfalsz.screen.DialogAnswer$1.onFinish(DialogAnswer.java:36)
at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)

这是我的 DialogAnswer.java

public class DialogAnswer extends Activity {

   private static ImageView resultImage;
   private static Dialog dialog = null;

   public static void show(Context context, boolean fCorrect) {

       dialog = new Dialog(context, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
       dialog.setContentView(R.layout.dialog);
       resultImage = (ImageView) dialog.findViewById(R.id.result_image);

       if (fCorrect)
            resultImage.setImageResource(R.drawable.correct_image);
       else
            resultImage.setImageResource(R.drawable.incorrect_image);

       dialog.show();

        new CountDownTimer(700, 100) {
            public void onTick(long millisUntilFinished) {
            }
            public void onFinish() {
               dialog.dismiss(); //this is line 36
            }
        }.start();
        }
}

GameActivity.java 有时,当我去另一个 Activity 时,我的帖子顶部会出现这样的错误。我不知道如何解决这个问题,很难调试,因为它存在罕见的错误但它确实存在。

最佳答案

可能很多人都在用谷歌搜索这个,所以我可能会把我的 2p 放在:

不幸的是,人们使用 isShowing() 的示例无法正常工作,因为当 View 分离( Activity 已消失)时,它仍然可以返回 true。

如果你很懒,其他发帖者评论说将它包装在一个 try {} 中也确实在/most/情况下有效(尽管在少数情况下系统可能会关闭它并且异常仍然会导致强制-关闭你不能尝试{}轮,因为它发生在 android 代码中,而不是你的)

最好的解决方案是在 Activity 完成/关闭时关闭对话框。如果您尝试在您的异步任务运行时用户离开后关闭它(或者,电话响起并且为他们导航离开)那么您将获得 ViewNotAttached 异常。

关于android - View 未附加到窗口管理器,对话框关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19538282/

相关文章:

java - setColorFilter 问题

android - 响应图像不适用于 android 手机/chrome 浏览器

android - System.out resolveUri 在错误的位图 uri : https://. 上失败 .. 当我尝试将图片从 Web 插入到 ListView 时

android - 我可以在itext 2.1.7中使用Playstore出售我的应用程序吗

android - 我可以在抽屉导航打开或关闭时收听吗(例如 : listener like button onclick)?

android - 通过将我的应用程序设为主屏幕应用程序来禁用主页按钮

android - OSS 许可证插件在启动 OsLicensesMenuActivity 时崩溃

android - 如何在我的 android 应用程序中实现搜索框?

android - 错误 :This class should provide a default constructor (a public constructor with no arguments) [Instantiatable] in Android

Android HAL 使系统卡在 poll() 函数中