简体   繁体   English

Android:EditText TextChangedListener上的堆栈溢出错误

[英]Android: stack overflow error on EditText TextChangedListener

I get the StackOverflow error running this particular code: 运行此特定代码时,出现StackOverflow错误:

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(""); 我一次注释了每一行,发现罪魁祸首是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. 我以前用过它,这让我感到困惑,为什么一条特别简单的指令会导致如此严重的错误...其余错误消息与“ android.view.ViewGroup.addFocusables(ViewGroup.java:637)”及其他有关,但我怀疑它们是否定论。

What am I missing? 我想念什么?

如果您要从同一TextViewTextWatcher设置TextView的文本,那么您将设置一个无限循环,这将导致StackOverflowException

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("");
            }
        }

    }); 

You are having this problem because you are changing the text from your onChange listener. 您遇到此问题是因为您正在从onChange侦听器更改文本。 What happens is the user changes the text, which invokes your onTextChange method, which in turn changes the text and that again invokes onTextChange listener - and so on. 发生的事情是用户更改了文本,这将调用您的onTextChange方法,该方法又更改了文本,并再次调用了onTextChange侦听器-依此类推。 If you are required to change the text from the listener, then you need to somehow ignore text changes from your code and only handle the ones from the user. 如果需要从侦听器更改文本,则需要以某种方式忽略代码中的文本更改,而仅处理用户的更改。 For example, you can try something like this: 例如,您可以尝试如下操作:

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

this will ignore all changes where the edittext content has been erased (by your code). 这将忽略所有已删除(通过您的代码)edittext内容的更改。

you are calling settext method inside the onTextChanged listener. 您正在onTextChanged侦听器内调用settext方法。 this would cause the listener to fire the onTextChanged method again and again. 这将导致侦听器一次又一次触发onTextChanged方法。 this is causing the stackOverflow error 这导致stackOverflow错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM