Android – Displaying Dialogs From Background Threads
判断一下Activity是否在finishing就好了,否则万一Activity销毁了,这个Dialog没有所依附的对象,就会有下面的异常抛出
private Handler myHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case DISPLAY_DLG: if (!isFinishing()) { showDialog(MY_DIALOG); } break; } } };
android.view.WindowManager$BadTokenException: Unable to add window — token [email protected] is not valid; is your activity running?
at android.view.ViewRoot.setView(ViewRoot.java:468)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.view.Window$LocalWindowManager.addView(Window.java:424)
at android.app.Dialog.show(Dialog.java:239)
at android.app.Activity.showDialog(Activity.java:2488)
…
at android.os.Handler.dispatchMessage(Handler.java:99)
…
时间: 2024-12-17 13:38:10