1

I'm making a feature that redirects the user to another activity once the timer reaches zero. I tried to swap the places of these if-else statements but the app keeps crashing. What could be the problem?

This is my countdown time block:

private void startTimer(TextView timerTextView) {
        examTimer = new Timer();

        examTimer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                if(seconds == 0){
                    minsTotalTime--;
                    seconds = 59;
                }

                else if (seconds == 0 && minsTotalTime == 0){
                    examTimer.purge();
                    examTimer.cancel();

                    Toast.makeText(ExamActivity.this, "Time ran out", Toast.LENGTH_SHORT).show();

                    Intent intent = new Intent(ExamActivity.this, ExamResults.class);
                    intent.putExtra("correct", getCorrectAnswers());
                    intent.putExtra("incorrect", getIncorrectAnswers());
                    startActivity(intent);

                    finish();
                }

                else {
                    seconds--;
                }
}
Kuraku
  • 11
  • 2
  • swap the order of the first and second condition. you should check if `if (seconds == 0 && minsTotalTime == 0)` first – galalem Apr 29 '23 at 17:58
  • BTW, why not use `CountDownTimer` instead ? [See this question](https://stackoverflow.com/questions/10032003/how-to-make-a-countdown-timer-in-android) – galalem Apr 29 '23 at 18:00

1 Answers1

0

Change your code to the following:

private void startTimer(TextView timerTextView) {
        examTimer = new Timer();

        examTimer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run(){
                if(seconds == 0){
                    if (minsTotalTime == 0){
                       examTimer.purge();
                       examTimer.cancel();

                        Toast.makeText(ExamActivity.this, "Time ran out", Toast.LENGTH_SHORT).show();

                        Intent intent = new Intent(ExamActivity.this, ExamResults.class);
                        intent.putExtra("correct", getCorrectAnswers());
                        intent.putExtra("incorrect", getIncorrectAnswers());
                        startActivity(intent);

                        finish();
                    }

                    else{
                        minsTotalTime--;
                        seconds = 59;
                    }
                }

                else{
                    seconds--;
                }
   }

}

You can also use a CountDownTimer for your purpose. Example code:

new CountDownTimer(COUNT_DOWN_MILLISECONDS, 1000) {
    public void onTick(long millisUntilFinished) {
        //Code to be executed while the countdown is going on
    }

    public void onFinish() {
        //Code to be executed when the countdown has hit zero.
    }

}.start();

You have replace COUNT_DOWN_MILLISECONDS with the count down limit in milliseconds. For example, if you want to set a timer for 10 seconds then you can set COUNT_DOWN_MILLISECONDS to 10000 (10000ms = 10s).

Dinux
  • 644
  • 1
  • 6
  • 19