IndexOutOfBoundsException on text

I use textwatcher to validate user input. Some of my users have crashed due to this text commentator. Here is the stack that google defines:

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)

And my code is:

        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());
            }
        });

I no longer have information on how this happened. It works fine fine, and I could not reproduce this error. Any idea what happened?

+4
source share
1 answer

The answer is already given in the commentary, but itโ€™s hard to understand how you simply reconciled, like me. So the real answer is:

Avoid adding TextWatcher two or more times, as the first one will edit the content, and the subsequent ones will throw an exception.

, 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;
}
+4

Source: https://habr.com/ru/post/1610724/


All Articles