简体   繁体   中英

Correctly dismissing progress dialog

I have a progress dialog that I need to dismiss and then finish the app. The way I'm doing it is like this:

new AlertDialog.Builder(MainActivity.this)
                            .setMessage("There has been a problem.")
                            .setPositiveButton("OK",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(
                                                DialogInterface dialog,
                                                int which) {
                                            dialog.dismiss();
                                            finish();
                                        }
                                    }).show();

Nevertheless, I always get the famous error:

Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@48189240 is not valid; is your activity running?
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.view.ViewRoot.setView(ViewRoot.java:505)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:200)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:114)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.app.Dialog.show(Dialog.java:241)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.app.ProgressDialog.show(ProgressDialog.java:107)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.app.ProgressDialog.show(ProgressDialog.java:90)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.app.ProgressDialog.show(ProgressDialog.java:85)

How could I dismiss it without problems? am I doing it right?

Thanks a lot in advance.

EDIT: I can get to show it perfectly, and even when dismissing it, the user doesn't see anything wrong, but in my LogCat I can see that nasty error.

I haven't tested this, though it should work. However, as an alternate, you may call finish() in your dialog's onClick and in your activity's onStop call dialog.dismiss() .

Calling dismiss in onStop will avoid the window leak warning message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM