IllegalArgumentException:视图未附加到窗口管理器。对话框.关闭

发布于 2024-11-06 07:02:49 字数 1449 浏览 0 评论 0原文

我有一个小错误想要摆脱。 我不知道为什么会出现这个错误。 模拟器和测试手机运行完美! 我拥有的唯一信息是我从 android Market 中的应用程序用户那里获得的 Stacktraces。

java.lang.IllegalArgumentException: View not attached to window manager
at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:355)
at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:200)
at android.view.Window$LocalWindowManager.removeView(Window.java:432)
at android.app.Dialog.dismissDialog(Dialog.java:278)
at android.app.Dialog.access$000(Dialog.java:71)
at android.app.Dialog$1.run(Dialog.java:111)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method

首先,我认为如果用户在进度对话框尚未关闭的情况下更改手机的方向,下面的代码将导致应用程序崩溃。 我通过添加 android:screenOrientation="portrait" 来确保方向不会改变。 但错误仍然存​​在。

有人可以帮我吗? 这是我使用的代码示例:

final ProgressDialog pd = ProgressDialog.show(this, "Title", "Message",  true, false);

new Thread(new Runnable(){
public void run(){
    makeHttpRequest();
    pd.dimiss();
}
}).start();

I have a little error i want to get rid of.
I have no idea why this error occurs.
The simulator and test phone runs perfect!
The only info I have is the Stacktraces I got from the app users in android Market.

java.lang.IllegalArgumentException: View not attached to window manager
at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:355)
at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:200)
at android.view.Window$LocalWindowManager.removeView(Window.java:432)
at android.app.Dialog.dismissDialog(Dialog.java:278)
at android.app.Dialog.access$000(Dialog.java:71)
at android.app.Dialog$1.run(Dialog.java:111)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method

First i thought the code below would cause the application to crash if the user change the phone's orientation while the progress dialog is not yet dismissed.
I made shure the orientation would not change by adding: android:screenOrientation="portrait".
But the error is still alive.

Could any one help me out?
This is a code example which i use:

final ProgressDialog pd = ProgressDialog.show(this, "Title", "Message",  true, false);

new Thread(new Runnable(){
public void run(){
    makeHttpRequest();
    pd.dimiss();
}
}).start();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随梦而飞# 2024-11-13 07:02:50

您是否将 android:screenOrientation="portrait" 放入 Activity 或应用程序中?当对话框未正确关闭时,当活动停止时,也会发生此异常,这意味着您应该在 onPause/onStop 中处理对话框关闭。我曾经遇到过这个问题。

Did you put android:screenOrientation="portrait" in activity or in appication? also this exception occurs when dialog is not properly dismiss, when activity stops, means you should handle dialog dismiss in onPause/ onStop. I have face this issue once.

撕心裂肺的伤痛 2024-11-13 07:02:50

即使您将方向限制为纵向,锁定屏幕也可能会变为横向,并且您的活动将被重新创建。为了确保对话框被关闭,我会这样做:

if (dialog != null && dialog.isShowing()) {
    dialog.cancel();
}

另外,当活动被销毁时,您应该关闭对话框:

@Override
protected void onDestroy() {    
    if (dialog != null && dialog.isShowing()) {
        dialog.cancel();
    }

    super.onDestroy();
}

这为我解决了问题 - 希望它有帮助:)

Even if you restrict the orientation to portrait, the lock screen for example might get into landscape and your activity is recreated. In order to make sure that the dialog gets dismissed, I would do this:

if (dialog != null && dialog.isShowing()) {
    dialog.cancel();
}

Also, you should dismiss the dialog when the activity is destroyed:

@Override
protected void onDestroy() {    
    if (dialog != null && dialog.isShowing()) {
        dialog.cancel();
    }

    super.onDestroy();
}

This solved the problem for me - hope it helps :)

巨坚强 2024-11-13 07:02:50

试试这个代码:

 update.setTitle(getResources().getString(R.string.app_name));
                update.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                update.setCancelable(true);
                update.setMax(100);
                update.show();


                Thread background = new Thread (new Runnable() {
                       public void run() {
                           try {
                               // enter the code to be run while displaying the progressbar.
                               //
                               // This example is just going to increment the progress bar:
                               // So keep running until the progress value reaches maximum value
                               while (update.getProgress()<= update.getMax()) {
                                   // wait 500ms between each update
                                   Thread.sleep(500);

                                   // active the update handler
                                   progressHandler.sendMessage(progressHandler.obtainMessage());
                               }

                           } catch (java.lang.InterruptedException e) {
                               // if something fails do something smart
                           }
                       }
                    });// start the background thread
                    background.start();
                    if(update.getProgress()== 100)
                       {
                           update.dismiss();
                       }
                }




        })
        .setNegativeButton("No", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

            }
        })
        .show();

Try this code:

 update.setTitle(getResources().getString(R.string.app_name));
                update.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                update.setCancelable(true);
                update.setMax(100);
                update.show();


                Thread background = new Thread (new Runnable() {
                       public void run() {
                           try {
                               // enter the code to be run while displaying the progressbar.
                               //
                               // This example is just going to increment the progress bar:
                               // So keep running until the progress value reaches maximum value
                               while (update.getProgress()<= update.getMax()) {
                                   // wait 500ms between each update
                                   Thread.sleep(500);

                                   // active the update handler
                                   progressHandler.sendMessage(progressHandler.obtainMessage());
                               }

                           } catch (java.lang.InterruptedException e) {
                               // if something fails do something smart
                           }
                       }
                    });// start the background thread
                    background.start();
                    if(update.getProgress()== 100)
                       {
                           update.dismiss();
                       }
                }




        })
        .setNegativeButton("No", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

            }
        })
        .show();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文