android – Textwatcher上的IndexOutOfBoundsException
作者:互联网
我正在使用textwatcher来检查用户输入的有效性.我的一些用户遇到了这个textwacher引起的崩溃.这是google给出的堆栈跟踪:
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());
}
});
我没有更多关于这是如何发生的信息.它大部分时间都可以正常工作,我无法重现这个bug.发生了什么事吗?
解决方法:
答案已在评论中给出,但很难看出你是否只是像我一样.所以真正的答案是:
避免添加TextWatcher两次或更多次,因为第一次将编辑内容,随后的内容将导致异常.
由于无法检查EditText是否已经设置了TextWatcher,因此我使用布尔值构建了自己的解决方案,检查是否已添加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 来源: https://codeday.me/bug/20190519/1136564.html