程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> java-應用程序在 count 和 dialog box上崩潰

java-應用程序在 count 和 dialog box上崩潰

編輯:編程綜合問答
應用程序在 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?

最佳回答:


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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved