-1

I'm trying a simple Android application which has a EditText field. The App worked just fine until I added a TextChangedListener to the EditText. The app keeps stopping, and when I remove the Listener, it just works like nothing happened.

Any ideas about this problem?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

Please check you EditText that you initialise properly then call TextChangedListener.

editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

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

            }

            @Override
            public void afterTextChanged(Editable s) {
                //Do what ever you want
            }
        });
  • i did the same, all inside **protected void onCreate** and the app keep stopping unless i remove the **listener** !! – Øvõnłívìę Wæđôûđį Jan 14 '18 at 21:50
  • you should initialise your edit text before calling addTextChangedListener – Md Mobinur Rahman Jan 15 '18 at 05:58
  • i initialized it but nothing changed !! – Øvõnłívìę Wæđôûđį Jan 15 '18 at 06:56
  • `@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EditText et2 = (EditText)findViewById(R.id.et2); et2.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) {} @Override public void afterTextChanged(Editable s) { } }); }` – Øvõnłívìę Wæđôûđį Jan 15 '18 at 09:32
  • java.lang.RuntimeException: Unable to start activity ComponentInfo{Com.****.****.****/Com.****.****.****.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.addTextChangedListener(android.text.TextWatcher)' on a null object reference......Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.addTextChangedListener(android.text.TextWatcher)' on a null object reference – Øvõnłívìę Wæđôûđį Jan 15 '18 at 10:05
  • It didn't find any editText name et2 in your activity_main.xml layout. Please check your Edit text name in your layout file and initialise properly in your java file – Md Mobinur Rahman Jan 15 '18 at 10:39