gpt4 book ai didi

android - BadTokenException : Unable to add window, 您的 Activity 是否正在运行?

转载 作者:行者123 更新时间:2023-11-29 19:05:43 24 4
gpt4 key购买 nike

所以我看到了错误

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
}

为什么会这样?我检查 Activity 是否 isFinishing() 如果 Activity 未运行,它不应该尝试显示提示,对吗?

最佳答案

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();
}

关于android - BadTokenException : Unable to add window, 您的 Activity 是否正在运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47341137/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com