WindowManagerBadTokenException无法添加窗口

时间:2013-04-26 16:45:42

标签: java android

我在下面有一个错误:

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

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

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

1 个答案:

答案 0 :(得分: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();   
}