29

I developed one simple app, like subtraction, addition. In this app I use three EditTexts, one for answer and other two for question. I want to calculate the answer of question on text change event. But when I apply the text change event on both of this the event occur but not properly work. Because when I enter in the text in first EditText of question the event occur but it throws this exception:

07-03 16:39:48.844: E/EduApp Log :=>(12537): Error In Text change Event java.lang.NumberFormatException: unable to parse '' as integer

What do I do? I use the TextWatcher for text change event.

txtOne.addTextChangedListener(this);
txtTwo.addTextChangedListener(this);

public void afterTextChanged(Editable s) {}

public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

public void onTextChanged(CharSequence s, int start, int before, int count) {}  
Sam
  • 84,460
  • 18
  • 172
  • 171
Zala Janaksinh
  • 2,871
  • 5
  • 29
  • 58

7 Answers7

50

I think you are receiving empty String " " which is causing this problem. Make sure you get a non-empty String from your EditText.

Consider your EditText doesn't have any value typed in, and you are trying to get its value and convert into int you will run into this kind of problem.

edittext.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
            if(!s.equals("") ) { 
                //do your work here 
            }
    }



    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {

    }

    public void afterTextChanged(Editable s) {

    }
});

Also check this link for more idea,

https://stackoverflow.com/a/3377648/603744

Paul Chu
  • 1,209
  • 3
  • 16
  • 23
Andro Selva
  • 51,960
  • 51
  • 189
  • 237
2

i think the best in case the edittext type is number... use the (length function) of parameter instead of (equle() function) ex:

edittext.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        if (s.length() > 0)
                { //do your work here }
        }

    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {

    }

    public void afterTextChanged(Editable s) {

    }
});
user6757534
  • 161
  • 1
  • 3
1

I used this and it's correct:

public void afterTextChanged(Editable arg0) {
    String inputs = input.getText().toString();
    Integer index=0;
    if(!inputs.equals("")) 
        index=Integer.valueOf(inputs);

}
Steven Penny
  • 82,115
  • 47
  • 308
  • 348
Omid
  • 19
  • 1
  • doesn't look different from the other answer, does it ;-) And will still throw a ParseException if the field content isn't ... parseable. – kleopatra Jan 20 '13 at 15:23
0

I think you need to check your editText value is empty or not first. Something like this:

String textValue;
textValue = edittext().getText().toString());
Log.v("","Value is " + textValue);
if(textValue != ""){
   // Call Text Change Listener Here
}else{
   // Throw error message or something
}

Hope it's help.

myo htet aung
  • 107
  • 2
  • 12
0

Added Kotlin implementation for future reference

 webUrl.editText!!.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

                var urlToload: String
                if (s ==null || s.isEmpty()) {
                    urlToload = UniversalWebViewFragment.YOUTUBE_SERACH_URL + "hd trailers"
                } else {
                    urlToload = UniversalWebViewFragment.GOOGLE_SERACH_URL + s.toString()
                }

                loadURL(urlToload)
            }

        })
Hitesh Sahu
  • 31,496
  • 11
  • 150
  • 116
0

I had the same issue and the problem was solved with these 2 actions:
1 - android:inputType="textNoSuggestions" (it did not solve the problem itself)
2 - adding the listener:

edittext1.addTextChangedListener(new TextWatcher() {

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

                Log.e("JFT", "EDITTEXT => = "+s);
                if(!s.equals("") ) {
                    //do your work here
                }
            }



            public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {

            }

            public void afterTextChanged(Editable s) {

            }
        });
AndroidHV
  • 369
  • 1
  • 9
0

For Kotlin use this below code

edit1.addTextChangedListener(object : TextWatcher {
   override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
       if (edit1.text.toString().length == 1) {
             edit2.requestFocus()
       }
   }

   override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int
   ) {// TODO Auto-generated method stub
   }

   override fun afterTextChanged(s: Editable) {}
})
Aftab Alam
  • 1,193
  • 9
  • 12