2 csh 34 csh_34 于 2013.05.30 13:16 提问

应用程序在 count 和 dialog box上崩溃

我使用下面的代码:

public class MyCount extends CountDownTimer
   {
        public MyCount(long millisInFuture, long countDownInterval) 
        {
            super(millisInFuture, countDownInterval);
        }
        @Override
        public void onFinish() 
        {
             displayAlertForRestart();
        }
        @Override
        public void onTick(long millisUntilFinished) 
        {
            counter.setText("Time Left: " + millisUntilFinished/1000 + " sec");
        }
     }
   public void displayAlertForRestart()
   {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setMessage("Do you want to Restart?");
    builder.setTitle("Game Over");
    builder.setInverseBackgroundForced(true);
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
    {
      public void onClick(DialogInterface dialog, int which) {

            Intent myIntent = new Intent(context,Level.class);
            startActivity(myIntent);
            dialog.dismiss();
            finish();
      }  
    });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {

      public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
        finish();
      }
    });
    AlertDialog alert = builder.create();
    alert.show();
   }

alert.show();处的 LogCat error:

36: E/AndroidRuntime(9829): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@43cf1e38 is not valid; is your activity running?

2个回答

xiaoyaovsxin
xiaoyaovsxin   2013.06.01 17:41
已采纳

AlertDialog.Builder builder = new AlertDialog.Builder(this);
这句话有问题,你应该在public MyCount(long millisInFuture, long countDownInterval)
加个Context,public MyCount(Context context,long millisInFuture, long countDownInterval)
然后再将context的值传给new AlertDialog.Builder(context);

wurenbi45
wurenbi45   2013.05.30 14:06

AlertDialog.Builder builder = new AlertDialog.Builder(this);
参数应该是一个activity的引用.

Csdn user default icon
上传中...
上传图片
插入图片