我正在使用textwatcher来检查用户输入的有效性。我的一些用户因此textwacher崩溃。这是google给的stacktrace:
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at android.widget.TextView.sendAfterTextChanged(TextView.java:7997)
at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:10043)
at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:679)
at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:437)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:333)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
和我的代码:
Zipcode.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (editable.toString().equals(zc)) return;
zc = editable.toString();
isZipcodeChecked = false;
Log.d(TAG, "Text changed : " + editable.toString());
if (editable.toString().length() != 5) {
Zipcode.setTextColor(Color.RED);
return;
}
checkZipcode(editable.toString());
}
});
我没有更多有关此情况的信息。在大多数情况下,它都能正常工作,而且我无法重现此错误。关于发生的事情有什么想法吗?
最佳答案
答案已经在评论中给出,但是很难看出您是否只是像我一样在略读。因此,真正的答案是:
避免添加TextWatcher两次或多次,因为第一个将编辑内容,随后的一个将导致异常。
由于无法检查EditText是否已经设置了TextWatcher,因此,我使用 bool 值构建了自己的解决方案,该 bool 值用于检查是否已添加TextWatcher。
private EditText mEditText;
private TextWatcher mTextWatcher;
private boolean mTextWatcherIsEnabled = false;
public void setTextWatcher(TextWatcher textWatcher) {
mTextWatcher = textWatcher;
}
public void enableTextWatcher() {
if (!mTextWatcherIsEnabled) {
mEditText.addTextChangedListener(mTextWatcher);
}
mTextWatcherIsEnabled = true;
}
public void disableTextWatcher() {
if (mTextWatcherIsEnabled) {
mEditText.removeTextChangedListener(mTextWatcher);
}
mTextWatcherIsEnabled = false;
}
关于android - Textwatcher上的IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33011637/