gpt4 book ai didi

java - WindowManagerBadTokenException 无法添加窗口

转载 作者:行者123 更新时间:2023-11-30 03:38:28 24 4
gpt4 key购买 nike

我有以下错误:

  //TODO tryAgain
private void tryAgain(){
new AlertDialog.Builder(this)
.setTitle("Game Over")
.setMessage("Times Up!")
.setPositiveButton("Try Again?", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
Intent retry = getIntent();
finish();
startActivity(retry);
}
})
.setNegativeButton("Back to Menu", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
Intent i = new Intent(Stage1_3.this, ShapingColors.class);
finish();
startActivity(i);
}
})
.show();
}

基本上我想做的是取消对话框,然后重新启动我当前的 Activity 和另一个我想返回到菜单 Activity 的 Activity 。我在这里错过了什么?感谢您的帮助。

更新 LOGCAT:

   04-27 00:42:44.680: E/AndroidRuntime(330): FATAL EXCEPTION: main
04-27 00:42:44.680: E/AndroidRuntime(330): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@40515dc0 is not valid; is your activity running?
04-27 00:42:44.680: E/AndroidRuntime(330): at android.view.ViewRoot.setView(ViewRoot.java:527)
04-27 00:42:44.680: E/AndroidRuntime(330): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
04-27 00:42:44.680: E/AndroidRuntime(330): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
04-27 00:42:44.680: E/AndroidRuntime(330): at android.view.Window$LocalWindowManager.addView(Window.java:424)
04-27 00:42:44.680: E/AndroidRuntime(330): at android.app.Dialog.show(Dialog.java:241)
04-27 00:42:44.680: E/AndroidRuntime(330): at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
04-27 00:42:44.680: E/AndroidRuntime(330): at com.say.shapingcolor.Stage1_3.tryAgain(Stage1_3.java:307)
04-27 00:42:44.680: E/AndroidRuntime(330): at com.say.shapingcolor.Stage1_3.access$0(Stage1_3.java:287)
04-27 00:42:44.680: E/AndroidRuntime(330): at com.say.shapingcolor.Stage1_3$MyCount.onFinish(Stage1_3.java:319)
04-27 00:42:44.680: E/AndroidRuntime(330): at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118)
04-27 00:42:44.680: E/AndroidRuntime(330): at android.os.Handler.dispatchMessage(Handler.java:99)
04-27 00:42:44.680: E/AndroidRuntime(330): at android.os.Looper.loop(Looper.java:123)
04-27 00:42:44.680: E/AndroidRuntime(330): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-27 00:42:44.680: E/AndroidRuntime(330): at java.lang.reflect.Method.invokeNative(Native Method)
04-27 00:42:44.680: E/AndroidRuntime(330): at java.lang.reflect.Method.invoke(Method.java:507)
04-27 00:42:44.680: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-27 00:42:44.680: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-27 00:42:44.680: E/AndroidRuntime(330): at dalvik.system.NativeStart.main(Native Method)

最佳答案

当您为不再存在的上下文显示对话框时,可能会发生这种情况。当您调用对话框的 show() 时, Activity 将被销毁。这通常发生在异步操作之后。确保您的 Activity (您要在其中显示对话框)正在运行。

不要在完成后调用该 Activity ,只需创建一个新的 Intent 并启动该 Activity 即可。这样做:

private void tryAgain(){
new AlertDialog.Builder(this)
.setTitle("Game Over")
.setMessage("Times Up!")
.setPositiveButton("Try Again?", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
Intent retry = new Intent(this, Stage1_3.class);//I guess Stage1_3 is the current activity
finish();
startActivity(retry);
}
})
.setNegativeButton("Back to Menu", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
Intent i = new Intent(Stage1_3.this, ShapingColors.class);
finish();
startActivity(i);
}
})
.show();
}

关于java - WindowManagerBadTokenException 无法添加窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16241359/

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