Android BadTokenException:无法添加窗口,您的活动正在运行吗?
所以我看到了错误Android BadTokenException:无法添加窗口,您的活动正在运行吗?,android,Android,所以我看到了错误 23 android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@3970aa84 is not valid; is your activity running? 24 at android.view.ViewRootImpl.setView(ViewRootImpl.java:562) 25 at andro
23 android.view.WindowManager$BadTokenException: Unable to add window --
token android.os.BinderProxy@3970aa84 is not valid; is your activity running?
24 at android.view.ViewRootImpl.setView(ViewRootImpl.java:562)
25 at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:282)
26 at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
27 at android.app.Dialog.show(Dialog.java:298)
28 at com.redacted.timerapp.TimerActivity.u(TimerActivity.java:1177)
29 at com.redacted.timerapp.TimerActivity.onStart(TimerActivity.java:271)
据报道,大约有0.1%的病例(大约每天10例)出现这种情况,但我无法重现这种情况。该方法的代码如下所示:
private void showDonePrompt() {
if (isFinishing()) return;
Dialog donePrompt = new Dialog(this, R.style.darkDialogDone);
donePrompt.requestWindowFeature(Window.FEATURE_NO_TITLE);
donePrompt.setContentView(R.layout.dialog_dark_done);
donePrompt.setCancelable(false);
Button btnRepeat = (Button)donePrompt.findViewById(R.id.btnRepeat);
btnRepeat.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// some DB operations, etc
donePrompt.dismiss();
}
}
Button btnStop = (Button)donePrompt.findViewById(R.id.btnStop);
btnStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// some DB operations, etc
donePrompt.dismiss();
}
}
donePrompt.show(); // this is line 1177 which presumably causes it
}
为什么会这样?我检查活动
isFinishing()
,如果活动未运行,它不应该尝试显示提示,对吗?您的isFinishing的源代码
/**
* Check to see whether this activity is in the process of finishing,
* either because you called {@link #finish} on it or someone else
* has requested that it finished. This is often used in
* {@link #onPause} to determine whether the activity is simply pausing or
* completely finishing.
*
* @return If the activity is finishing, returns true; else returns false.
*
* @see #finish
*/
public boolean isFinishing() {
return mFinished;
}
因此,如果isFinishing
为true,则活动将被销毁
你可以试试这个
if(!isFinishing()){
donePrompt.show();
}
或
此崩溃通常是由于您的应用程序试图使用以前完成的活动作为上下文显示对话框而导致的
例如,如果某个活动触发一个异步任务,该任务在完成时尝试显示对话框,但用户在任务完成之前从该活动返回,则可能会发生这种情况
if (!isFinishing()) {
//showdialog here
}
我的方法的第一行已经是isFinishing()
check。考虑到这种情况可能千分之一,如果不更新应用程序并等待几天,我无法确认您的其他解决方案是否有效。
if (!isFinishing()) {
//showdialog here
}