应用程序崩溃计数和对话框

时间:2012-05-16 13:02:08

标签: java android

我正在使用以下代码。

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

LogCat错误:alert.show();

时出错
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 个答案:

答案 0 :(得分:0)

像这样更改您的代码

public class MyCount extends CountDownTimer
   {

   Context mContext;

        public MyCount(long millisInFuture, long countDownInterval,Context context) 
        {
            super(millisInFuture, countDownInterval);

            mContext=context;
        }

        @Override
        public void onFinish(Context context) 
        {
             displayAlertForRestart(context);
        }

        @Override
        public void onTick(long millisUntilFinished) 
        {
            counter.setText("Time Left: " + millisUntilFinished/1000 + " sec");
        }

     }


   public void displayAlertForRestart(Context context)
   {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    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();
   }

答案 1 :(得分:0)

如果你的倒计时没有完成,你必须先完成它才能完成活动。

完成活动后,请尝试myCount.cancel