Search code examples
androidandroid-edittextstack-overflow

Android: stack overflow error on EditText TextChangedListener


I get the StackOverflow error running this particular code:

litera.addTextChangedListener(new TextWatcher(){
            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){
                String ghici = litera.getText().toString();
                System.out.println(ghici);
                litera.setText("");
            }

        }); 

I commented each line at a time and I found out that the culprit is the litera.setText(""); line, the others work normally. I've used it before and it baffles me why a particularly simple instruction causes such a bad error... The rest of the error messages are about "android.view.ViewGroup.addFocusables(ViewGroup.java:637)" and others, but I doubt they are conclusive.

What am I missing?


Solution

  • u have to use this way

     litera.addTextChangedListener(new TextWatcher(){
            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){
                String strEnteredVal = litera.getText().toString();
                if(!strEnteredVal.equals("")){
                System.out.println(strEnteredVal);
                litera.setText("");
                }
            }
    
        });