java - 应用程序因计数和对话框而崩溃

标签 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?

最佳答案

像这样改变你的代码

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

关于java - 应用程序因计数和对话框而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10619145/

相关文章:

java - php 中的 AES/CBC/PKCS5Padding java 等价物是什么?

java - Maven:如何处理包含模块的模块?

java - 使用 maven tycho 构建 list 条目 Bundle-ClassPath,该 list 条目由 maven-dependency-plugin 下载

android - Android 4.0 添加日历和事件

java - xml 中的字符串标记在 16 以上的 api 中产生了一些问题

java - ARToolKit:在移动设备上处理自然特征跟踪图像 (jpeg)

java - Apache IVY 错误消息? : impossible to get artifacts when data has not been loaded

java.lang.UnsatisfiedlinkError : Native method not found

android - 如何在两个不同的android应用程序之间进行通信

java - Android Google Maps API 不适用于 debug.keystore