Android CountDownTimer在onFinish()里面打开一个对话框

时间:2016-12-16 20:21:38

标签: java android countdowntimer

onfinish()为我的游戏countDownTimer打开一个对话框时,我的项目崩溃了。 代码如下:

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 android.os.BinderProxy@ca15ab7 is not valid; is your activity running?

1 个答案:

答案 0 :(得分:0)

您正在尝试显示不再存在的上下文的对话框 在你的情况下 SeriesActivity 被销毁 尝试使用
final Dialog dialog = new Dialog(getActivity().this);