当我在 TextWatcher 中使用此代码时,我收到 stackoverflow
错误:
txt.setText(s.toString().substring(0, s.length()-1) + "a");
为什么会发生这种情况以及如何防止这种情况发生?
我在 onTextChanged
和 afterTextChanged
上使用它,它们都生成 stackoverflow
最佳答案
此代码已损坏。在伪代码中,它读取
//whenever the text changes, replace the last letter with an 'a'
设置文本是文本更改,即使新文本与旧文本匹配,因此每次将“a”替换为另一个“a”时,您都会递归直到堆栈已满。
如果有终端情况,比如
if (!txt.endsWith("a"))
这样就不会溢出。
关于java - android TextWatcher 中的 stackoverflow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15660093/