Search code examples
javaandroidcountdowntimer

Is parsing a string in the onTick method crashing my app?


I have a short CountDownTimer (3 seconds long at minimum, 10 at max) that I'm using to control when a new math problem is displayed on the screen. Every 1 second, the CountDownTimer should check if the user has entered the correct answer in an EditText. Here is the code I have for it:

private void startQuestionTimer()
    {
        long startTime = DataHolder.getQuestionTime()*1000;
        questionTimer = new CountDownTimer(startTime, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                //continually check user's input into the EditText
                //if input is ever equal to actualIntAnswer, reset the timer and EditText
                //then display another math problem
                int givenAnswer = Integer.parseInt(userAnswer.getText().toString());
                if (givenAnswer == actualIntAnswer)
                {
                    questionTimer.cancel();
                    userAnswer.getText().clear();
                    displayNewMathProblem();
                    questionTimer.start();
                }
            }

            @Override
            public void onFinish() {
                displayNewMathProblem();
                if (!testTimeExpired)
                {
                    questionTimer.start();
                }
                else
                {
                    questionTimer.cancel();
                }
            }
        }.start();
    }

The problem I'm having is that the app crashes as soon as this CountDownTimer is created. For some reason, int givenAnswer = Integer.parseInt(userAnswer.getText().toString()); seems to crash the app, and I think it's because the CountDownTimer cannot do all that in just 1 second. I tried splitting it into 2 methods like so:

private int givenAnswer;
private String givenAnswerToString()
    {
        return userAnswer.getText().toString();
    }

    private void givenAnswerToInt()
    {
        givenAnswer = Integer.parseInt(givenAnswerToString());
    }

And then calling to givenAnswerToInt() in onTick() like so:

@Override
            public void onTick(long millisUntilFinished) {
                givenAnswerToInt();
                if (givenAnswer == actualIntAnswer)
                {
                    questionTimer.cancel();
                    userAnswer.getText().clear();
                    displayNewMathProblem();
                    questionTimer.start();
                }

            }

Here is the error message I get when the CountDownTimer is created: enter image description here

What's going on here? Is it an issue of trying to do too many things in a short amount of time like I think it is, or is it something else entirely? What can I do to fix this?


Solution

  • Integer.parseInt can throw a NumberFormatException which would cause the application to stop abruptly if not caught.
    This happens when the String that you're passing as parameter of parseInt isn't recognized as a decimal number.
    The fact that this happens almost instantaneously in your code is due to the fact that the EditText.getText().toString() is just returning "", since the user has not typed any number in the widget.
    What you can do is simply manage the exception with a try-catch statement:

    int givenAnswer = 0; // initialize accordingly with your needs
    try {
        givenAnswer = Integer.parseInt(userAnswer.getText().toString());
    } catch (NumberFormatException e) {
        // return or do something else to deal with the problem
    }
    
    if(givenAnswer == actualIntAnswer){
        ...
    }
    ...