所以我看到了错误
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()
是否曾经尝试显示活动未运行的提示,对吧?
答案 0 :(得分:0)
您是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
为真,则Activity
将被销毁。
你可以试试这个。
if(!isFinishing()){
donePrompt.show();
}
或
if(!hasWindowFocus()){
donePrompt.show();
}
答案 1 :(得分:0)
此崩溃通常是由于您的应用尝试使用先前完成的“活动”作为上下文来显示对话框。
例如,如果活动触发一个AsyncTask并在完成时尝试显示一个对话框,但是用户在任务完成之前从活动导航返回,则会发生这种情况。
if (!isFinishing()) {
//showdialog here
}