我创建了EditText
并将其设置为 addtextchangedlistener 。
代码是:
shared_tags.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
shared_tags.setText( shared_tags.getText().toString().toLowerCase().replace(" ", "#"));
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
但它给出了以下错误:
03-13 23:22:03.384: E/InputEventReceiver(19895): Exception dispatching input event.
03-13 23:22:03.384: E/MessageQueue-JNI(19895): Exception in MessageQueue callback: handleReceiveCallback
03-13 23:22:03.424: E/MessageQueue-JNI(19895): java.lang.StackOverflowError
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.graphics.Paint.getTextRunAdvances(Paint.java:1754)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.graphics.Paint.getTextRunAdvances(Paint.java:1720)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.text.MeasuredText.addStyleRun(MeasuredText.java:172)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.text.MeasuredText.addStyleRun(MeasuredText.java:212)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.text.StaticLayout.generate(StaticLayout.java:325)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.text.DynamicLayout.reflow(DynamicLayout.java:284)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.text.DynamicLayout.<init>(DynamicLayout.java:170)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.makeSingleLayout(TextView.java:6314)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.makeNewLayout(TextView.java:6208)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.checkForRelayout(TextView.java:6755)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3923)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3781)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.EditText.setText(EditText.java:99)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3756)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at com.wub.twostrangers.share_image$3.onTextChanged(share_image.java:236)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.sendOnTextChanged(TextView.java:7584)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3926)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3781)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.EditText.setText(EditText.java:99)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3756)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at com.wub.twostrangers.share_image$3.onTextChanged(share_image.java:236)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.sendOnTextChanged(TextView.java:7584)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3926)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3781)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.EditText.setText(EditText.java:99)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3756)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at com.wub.twostrangers.share_image$3.onTextChanged(share_image.java:236)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.sendOnTextChanged(TextView.java:7584)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3926)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3781)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.EditText.setText(EditText.java:99)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3756)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at com.wub.twostrangers.share_image$3.onTextChanged(share_image.java:236)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.sendOnTextChanged(TextView.java:7584)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3926)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3781)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.EditText.setText(EditText.java:99)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3756)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at com.wub.twostrangers.share_image$3.onTextChanged(share_image.java:236)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.sendOnTextChanged(TextView.java:7584)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3926)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3781)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.EditText.setText(EditText.java:99)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3756)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at com.wub.twostrangers.share_image$3.onTextChanged(share_image.java:236)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.sendOnTextChanged(TextView.java:7584)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3926)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3781)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.EditText.setText(EditText.java:99)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3756)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at com.wub.twostrangers.share_image$3.onTextChanged(share_image.java:236)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.sendOnTextChanged(TextView.java:7584)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3926)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3781)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.EditText.setText(EditText.java:99)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3756)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at com.wub.twostrangers.share_image$3.onTextChanged(share_image.java:236)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.sendOnTextChanged(TextView.java:7584)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3926)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3781)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.EditText.setText(EditText.java:99)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3756)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at com.wub.twostrangers.share_image$3.onTextChanged(share_image.java:236)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.sendOnTextChanged(TextView.java:7584)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3926)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.widget.TextView.setText(TextView.java:3781)
03-13 23:22:03.424: E/MessageQueue-JNI(19895): at android.wid
03-13 23:22:03.424: D/AndroidRuntime(19895): Shutting down VM
03-13 23:22:03.424: W/dalvikvm(19895): threadid=1: thread exiting with uncaught exception (group=0x4132fac8)
03-13 23:22:03.434: E/AndroidRuntime(19895): FATAL EXCEPTION: main
03-13 23:22:03.434: E/AndroidRuntime(19895): java.lang.StackOverflowError
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.graphics.Paint.getTextRunAdvances(Paint.java:1754)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.graphics.Paint.getTextRunAdvances(Paint.java:1720)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.text.MeasuredText.addStyleRun(MeasuredText.java:172)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.text.MeasuredText.addStyleRun(MeasuredText.java:212)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.text.StaticLayout.generate(StaticLayout.java:325)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.text.DynamicLayout.reflow(DynamicLayout.java:284)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.text.DynamicLayout.<init>(DynamicLayout.java:170)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.makeSingleLayout(TextView.java:6314)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.makeNewLayout(TextView.java:6208)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.checkForRelayout(TextView.java:6755)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3923)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3781)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.EditText.setText(EditText.java:99)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3756)
03-13 23:22:03.434: E/AndroidRuntime(19895): at com.wub.twostrangers.share_image$3.onTextChanged(share_image.java:236)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.sendOnTextChanged(TextView.java:7584)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3926)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3781)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.EditText.setText(EditText.java:99)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3756)
03-13 23:22:03.434: E/AndroidRuntime(19895): at com.wub.twostrangers.share_image$3.onTextChanged(share_image.java:236)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.sendOnTextChanged(TextView.java:7584)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3926)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3781)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.EditText.setText(EditText.java:99)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3756)
03-13 23:22:03.434: E/AndroidRuntime(19895): at com.wub.twostrangers.share_image$3.onTextChanged(share_image.java:236)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.sendOnTextChanged(TextView.java:7584)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3926)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3781)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.EditText.setText(EditText.java:99)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3756)
03-13 23:22:03.434: E/AndroidRuntime(19895): at com.wub.twostrangers.share_image$3.onTextChanged(share_image.java:236)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.sendOnTextChanged(TextView.java:7584)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3926)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3781)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.EditText.setText(EditText.java:99)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3756)
03-13 23:22:03.434: E/AndroidRuntime(19895): at com.wub.twostrangers.share_image$3.onTextChanged(share_image.java:236)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.sendOnTextChanged(TextView.java:7584)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3926)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3781)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.EditText.setText(EditText.java:99)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3756)
03-13 23:22:03.434: E/AndroidRuntime(19895): at com.wub.twostrangers.share_image$3.onTextChanged(share_image.java:236)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.sendOnTextChanged(TextView.java:7584)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3926)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3781)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.EditText.setText(EditText.java:99)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3756)
03-13 23:22:03.434: E/AndroidRuntime(19895): at com.wub.twostrangers.share_image$3.onTextChanged(share_image.java:236)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.sendOnTextChanged(TextView.java:7584)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3926)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3781)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.EditText.setText(EditText.java:99)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3756)
03-13 23:22:03.434: E/AndroidRuntime(19895): at com.wub.twostrangers.share_image$3.onTextChanged(share_image.java:236)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.sendOnTextChanged(TextView.java:7584)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3926)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3781)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.EditText.setText(EditText.java:99)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3756)
03-13 23:22:03.434: E/AndroidRuntime(19895): at com.wub.twostrangers.share_image$3.onTextChanged(share_image.java:236)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.sendOnTextChanged(TextView.java:7584)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3926)
03-13 23:22:03.434: E/AndroidRuntime(19895): at android.widget.TextView.setText(TextView.java:3
我的布局xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<EditText
android:id="@+id/shared_tags"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:hint="@string/prompt_shared_tags"
android:singleLine="true"
android:maxLength="70"
/>
</RelativeLayout>
当我删除addtextchangedlistener时,它可以工作,如果我想把它写回我的edittext,它会给出这个错误
我编辑了我的问题,我理解了这个问题,我想让我的编辑框的keylistener检测到用户按下空格,但是当用户立即按空格键时我想要替换,但是当我在我的编辑框上设置文本时它将是递归的,我该如何解决?**
我该如何解决?
感谢
答案 0 :(得分:1)
我通过这样做解决了我的问题 - 修改你的onTextChangedMethod如下(原因如下):
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
String str = shared_tags.getText().toString();
if(!str.equals(""))
shared_tags.setText("");
// If you want to do something else - do it here
// Not sure if this will work to identify space - never tried it
if(!str.contains(" ")) // if this doesn't work try KeyEvent's getAction() method
shared_tags.setText("Whatever you want");
}
这背后的原因有点奇怪(可能是一个错误) - 当没有输入任何文本时,onTextChangedMethod()继续调用自身,虽然它什么都不做,但连续调用自身会导致堆栈溢出并抛出此错误。由于onTextChanged()方法,无限递归是原因。如果你调试,你会看到这个。
编辑:在您更新的日志中,您可以看到我刚刚说过的内容。您的sendOnTextChanged()方法将被重复调用。