在Android的嵌套倒计时(Nesting countdown timers in android

2019-10-17 23:37发布

我想打电话CountDownTimer当一个countdownTimer完成它的execution.I试着打电话给一个countdowntimer从另一个countdown timeronFinish()方法,但它给我的nullpointer exception ,当我运行这段代码。

public void First_Timer(){
    Timer_Off =false;


    dialog = new Dialog(context,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
    dialog.setCancelable(false);
    dialog.setContentView(R.layout.backwash_timer_dialog);


    final ToggleButton togglebtn_BW_timer = (ToggleButton)dialog.findViewById(R.id.togglebtn_BW_timer);
    final ProgressBar bar = (ProgressBar)dialog.findViewById(R.id.progressBar1);
     textView1 = (TextView)dialog.findViewById(R.id.textView1);
     btn_BW_timer_stop = (Button)dialog.findViewById(R.id.btn_BW_timer_stop);
    /** CountDownTimer starts with 2 minutes and every onTick is 1 second */
    cdt = new CountDownTimer(twoMin, 1000) { 

        public void onTick(long millisUntilFinished) {


            bar.setProgress((int) ((millisUntilFinished / 1000) ));
           messageHandler.sendMessage(Message.obtain(messageHandler,(int) ((millisUntilFinished / 1000) ) ));

        }

        public void onFinish(){
            textView1.setText("Time Up! ");
            cdt.cancel();
            Timer_Off =true;
            dialog.dismiss();

           // Call Second Timer Here

            Second_Timer();


        }
    }.start();







    btn_BW_timer_stop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            cdt.cancel();
            bar.setProgress(0);
            textView1.setText("0");
            Timer_Off=true;
            dialog.dismiss();
            //onBackwashStopped();
        }
    });
    dialog.show();


}

和代码的另一定时器,

    public void Second_Timer(){
    Timer_Rinse_Off =false;


    dialog_Rinse = new Dialog(context,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
    dialog_Rinse.setCancelable(false);
    dialog_Rinse.setContentView(R.layout.rinse_timer_dialog);






    final ProgressBar bar = (ProgressBar)dialog.findViewById(R.id.progressbar_Rinse);
    txtRinse_TimerVal = (TextView)dialog.findViewById(R.id.txtRinse_TimerVal);
     btn_Rinse_timer_stop = (Button)dialog.findViewById(R.id.btn_Rinse_timer_stop);
    /** CountDownTimer starts with 2 minutes and every onTick is 1 second */
    cdt_Rinse = new CountDownTimer(twoMin, 1000) { 

        public void onTick(long millisUntilFinished) {


            bar.setProgress((int) ((millisUntilFinished / 1000) ));
           messageHandler.sendMessage(Message.obtain(messageHandler,(int) ((millisUntilFinished / 1000) ) ));

        }

        public void onFinish(){
            txtRinse_TimerVal.setText("Time Up! ");
            Timer_Rinse_Off =true;
            dialog_Rinse.dismiss();
        }
    }.start();






    btn_Rinse_timer_stop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            cdt_Rinse.cancel();
            bar.setProgress(0);
            txtRinse_TimerVal.setText("0");
            Timer_Rinse_Off=true;
            dialog_Rinse.dismiss();
            //onBackwashStopped();
        }
    });
    dialog_Rinse.show();


}
文章来源: Nesting countdown timers in android