TextWatcher afterTextChanged causes stackoverflow in android

可紊 提交于 2019-12-10 13:57:16

问题


I have a method drawItems() which everytime creates a new layout and sets it as a contentView. And I also have a control EditText which should remove other elements when it's content is changed.

edit.addTextChangedListener(new TextWatcher() {

                    public void afterTextChanged(Editable s) {
                        currentText = s.toString();
                        drawItems();
                    }

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

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

What I want to do is save its current text, remove all elements and leave only this EditText with the saved string. When I'm trying to run this application the error raised is StackOverflow because it renders drawItems method infinite number of times. Why does it render drawItems within afterTextChanged even if I don't change its content? It's rendered even before the whole page is loaded.


回答1:


This method is called to notify you that, somewhere within s, the text has been changed. It is legitimate to make further changes to s from this callback, but be careful not to get yourself into an infinite loop, because any changes you make will cause this method to be called again recursively. (You are not told where the change took place because other afterTextChanged() methods may already have made other changes and invalidated the offsets. But if you need to know here, you can use setSpan(Object, int, int, int) in onTextChanged(CharSequence, int, int, int) to mark your place and then look up from here where the span ended up.

put:

public void afterTextChanged(Editable s) {
    if (MyEditText.getText().toString().compareTo(s.toString()) != 0)
    {
        // your code ...
    }
}



回答2:


This little snippet might help:

editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start,
                    int before, int count) {
            }

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

            @Override
            public void afterTextChanged(Editable s) {

                idAnswerEditText.removeTextChangedListener(this);

                String someString = StringUtil.doSomething(s.toString());
                idAnswerEditText.getText().clear();
                idAnswerEditText.getText().append(someString);
                idAnswerEditText.setSelection(someString.length());

                idAnswerEditText.addTextChangedListener(this);

            }

});

When you edit the String while the user is typing, remove the listener and do whatever you need to do on the text, then reattach the listener to the EditText.




回答3:


As @DroidIn.net said in a comment, you're probably triggering a text-change event in your afterTextChanged processing - probably in drawItems().

You can solve the stack-overflow exception by using a Handler to postpone the processing of drawItems, but that will probably just get you into an infinite loop. You should look at drawItems, and make sure they don't change your edit text, causing the change to change the edit text again and so on.



来源:https://stackoverflow.com/questions/8628437/textwatcher-aftertextchanged-causes-stackoverflow-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!