1

我在下面有一个错误:

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

基本上我想要做的是取消对话框然后重新启动我当前的活动和另一个我想回到菜单活动。我在这里缺少什么?任何帮助表示赞赏。

更新日志:

   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)
4

1 回答 1

1

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

完成后不要调用活动,只需创建一个新意图并启动活动。这样做:

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();   
}   
于 2013-04-26T17:00:07.613 回答