我试图实现textwatcher接口,我试图在afterTextChanged()
中更改文本,但它崩溃了。为什么会坠毁?:
@Override
public void afterTextChanged(Editable s) {
Editable myText;
if(s.toString().contains("hi")){
myText = s.append(" ho");
myEdit.setText(myText);
}
}
洛卡特
01-27 01:23:18.213: E/AndroidRuntime(2563): FATAL EXCEPTION: main
01-27 01:23:18.213: E/AndroidRuntime(2563): java.lang.StackOverflowError
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.graphics.Paint.getTextRunAdvances(Paint.java:1720)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.MeasuredText.addStyleRun(MeasuredText.java:164)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.MeasuredText.addStyleRun(MeasuredText.java:204)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.StaticLayout.generate(StaticLayout.java:281)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.DynamicLayout.reflow(DynamicLayout.java:284)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.DynamicLayout.access$000(DynamicLayout.java:35)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.DynamicLayout$ChangeWatcher.reflow(DynamicLayout.java:621)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.DynamicLayout$ChangeWatcher.onTextChanged(DynamicLayout.java:631)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:247)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:30)
01-27 01:23:18.213: E/AndroidRuntime(2563): at com.example.textfields.MainActivity$1.afterTextChanged(MainActivity.java:45)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.widget.TextView.sendAfterTextChanged(TextView.java:7108)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:8805)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:247)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:30)
01-27 01:23:18.213: E/AndroidRuntime(2563): at com.example.textfields.MainActivity$1.afterTextChanged(MainActivity.java:45)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.widget.TextView.sendAfterTextChanged(TextView.java:7108)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:8805)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:247)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:30)
01-27 01:23:18.213: E/AndroidRuntime(2563): at com.example.textfields.MainActivity$1.afterTextChanged(MainActivity.java:45)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.widget.TextView.sendAfterTextChanged(TextView.java:7108)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:8805)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:247)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:30)
01-27 01:23:18.213: E/AndroidRuntime(2563): at com.example.textfields.MainActivity$1.afterTextChanged(MainActivity.java:45)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.widget.TextView.sendAfterTextChanged(TextView.java:7108)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:8805)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:247)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:30)
01-27 01:23:18.213: E/AndroidRuntime(2563): at com.example.textfields.MainActivity$1.afterTextChanged(MainActivity.java:45)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.widget.TextView.sendAfterTextChanged(TextView.java:7108)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:8805)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:247)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:30)
01-27 01:23:18.213: E/AndroidRuntime(2563): at com.example.textfields.MainActivity$1.afterTextChanged(MainActivity.java:45)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.widget.TextView.sendAfterTextChanged(TextView.java:7108)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:8805)
01-27 01:23:18.213: E/AndroidRuntime(2563): at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
最佳答案
在设置文本之前,您应该在EditText
上调用removeTextChangedListener(),并在更改文本之后添加侦听器。否则,afterTextChanged()
就会像你注意到的那样一直被呼叫。
关于android - afterTextChanged崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14543354/