2016-12-16 57 views
0

崩潰我的項目,當我在onfinish()countDownTimer爲我的遊戲打開一個對話框。 的代碼如下:Android CountDownTimer崩潰onFinish()打開一個對話框

countDownTimer = new CountDownTimer(31000, 1000) { 
      public void onTick(long millisUntilFinished) { 
       String time = String.format("%02d",millisUntilFinished/1000); 
       tvCountdown.setText("00: " + time); 
      } 

      public void onFinish() { 
       tvCountdown.setText("00: 00"); 
       Log.d("tttt"," Called CountDownTimer onFinish"); 
       //timeUpDialog(); 

       resetTimer(); 
     /* custom dialog creating */ 
       final Dialog dialog = new Dialog(SeriesActivity.this); 
       dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
       dialog.setContentView(R.layout.dialog_correct_answer); 
       dialog.setCancelable(false); 

     /* set custom dialog component */ 
       ImageView ivWrong = (ImageView) dialog.findViewById(R.id.iv_correct_or_wrong); 
       ivWrong.setImageResource(R.drawable.time_up); 
       TextView tvWrong = (TextView) dialog.findViewById(R.id.tv_correct_or_wrong); 
       tvWrong.setText(getString(R.string.time_up)); 
       Button btnRetry = (Button) dialog.findViewById(R.id.iv_next_or_retry); 
       btnRetry.setTypeface(SplashScreenActivity.getRobotoFont(SeriesActivity.this)); 
       btnRetry.setText("Retry"); 

       btnRetry.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         startQuiz(); 
         dialog.dismiss(); 
        } 
       }); 
       dialog.show(); 
      } 
     }.start(); 

這裏是當應用程序崩潰日誌:

FATAL EXCEPTION: main       
Process: me.mortuza.guesswhat, PID: 19154                  
android.view.WindowManager$BadTokenException: Unable to add window -- token [email protected] is not valid; is your activity running? 

回答

0

你想顯示不再存在上下文的對話框。
您的情況系列活動被銷燬。
嘗試使用
final Dialog dialog = new Dialog(getActivity().this);