Is parsing a string in the onTick method crashing my app?
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){
...
}
...