xHero
xHero

Reputation: 1

Textwatchers afterTextChanged() keeps crashing

I've figured out that afterTextChanged() is called the everytime the EditText text changes (and thats why it crashes) but I don't know how to solve the problem. Can anyone please help me!

setting the edit text and adding addTextChangedListener

    editText= (EditText) findViewById(R.id.editText);
    editText.setInputType(InputType.TYPE_NULL);
    editText.addTextChangedListener(textWatcher);


    textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            //editText.removeTextChangedListener(textWatcher); //this doesn't help
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {


           // Toast.makeText(mainAct, "OK",Toast.LENGTH_SHORT).show();


            String user_answer  =editText.getText().toString();
            user_ans_int = Integer.parseInt(user_answer);

This should check if the text in the EditText is correct and this is also where the app crashes (without this part it runs perfectly)

            if (user_ans_int == answer){
                Toast.makeText(mainAct, "True", Toast.LENGTH_SHORT).show();
                editText.setText("");

            }
            else {
                Toast.makeText(mainAct, "Naaw - try again haha", Toast.LENGTH_SHORT).show();
                editText.setText("");//delete text after checking
                tru = false;
            }


        }

    };

}

Upvotes: 0

Views: 1162

Answers (3)

Adam
Adam

Reputation: 51

      // Write down few code inside a  afterTextChanged method 

      if(!s.equal("")){
        try {
        String user_answer = s.getText().toString();
        user_ans_int = Integer.parseInt(user_answer);
    } catch (NumberFormatException e) {
        e.printStackTrace();
    }

      }else{
            Toast.makeText(mainAct, "Please Enter Text",Toast.LENGTH_SHORT).show();
}

Upvotes: 0

Thanos
Thanos

Reputation: 3665

You need to disable the TextWatcher when you update the text in the if..else statement where you check for the correct answer. What you do right now is that you check if the answer is correct and then set the text back to "". That triggers again the text watcher because the text has changed. Then you try to parse to int an empty string. So change your if..else to this:

if (user_ans_int == answer){
     editText.removeTextChangedListener(textWatcher);
     Toast.makeText(mainAct, "True", Toast.LENGTH_SHORT).show();
     editText.setText("");
     editText.addTextChangedListener(textWatcher);
} else {
     editText.removeTextChangedListener(textWatcher);
     Toast.makeText(mainAct, "Naaw - try again haha", Toast.LENGTH_SHORT).show();
     editText.setText("");//delete text after checking
     tru = false;
     editText.addTextChangedListener(textWatcher);
}

Practically, you could also place a try..catch statement where you parse the content of the editText to an int to catch the exception but in this case you will call the textWatcher without needing to do so. I believe that disabling and re-enabling the textWatcher is much more efficient.

Upvotes: 1

Lei Guo
Lei Guo

Reputation: 2560

You will get a NumberFormatException here: user_ans_int = Integer.parseInt(user_answer); It's because you sign a None Number value to your editText and cause its afterTextChanged() being called.

I think you can change your code as following:

 user_ans_int = Integer.parseInt(user_answer);

to:

try{
 user_ans_int = Integer.parseInt(user_answer);
}catch(NumberFormatException e){
   // A none number value is signed to your edittext, do something here.
   return;
}

Upvotes: 2

Related Questions