gpt4 book ai didi

android - afterTextChanged 崩溃

转载 作者:太空狗 更新时间:2023-10-29 16:21:08 29 4
gpt4 key购买 nike

我试图实现 Textwatcher 接口(interface),我试图在 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)

最佳答案

你应该调用removeTextChangedListener()在设置文本之前在 EditText 上添加,并在更改文本后添加回监听器。否则 afterTextChanged() 将继续被调用,就像您已经注意到的那样。

关于android - afterTextChanged 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14543354/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com