4

I am using TextInputLayout and TextInputEditText in my app along with android:inputType="textAutoComplete" and I don't allow space between two words so if I select an item with a space in auto-complete drop-down or spellcheck, I run into an IndexOutOfBoundsException:

java.lang.IndexOutOfBoundsException: setSpan (0 ... 8) ends beyond length 7
            at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1265)
            at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:684)
            at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:677)
            at android.widget.TextView.setSpan_internal(TextView.java:9900)
            at android.widget.Editor.replaceWithSuggestion(Editor.java:2654)
            at android.widget.Editor.-wrap18(Editor.java)
            at android.widget.Editor$SuggestionsPopupWindow.onItemClick(Editor.java:3638)
            at android.widget.AdapterView.performItemClick(AdapterView.java:310)
            at android.widget.AbsListView.performItemClick(AbsListView.java:1156)
            at android.widget.AbsListView$PerformClick.run(AbsListView.java:3121)
            at android.widget.AbsListView$3.run(AbsListView.java:4036)
            at android.os.Handler.handleCallback(Handler.java:751)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:154)
            at android.app.ActivityThread.main(ActivityThread.java:6119)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

In my activity, I use a TextWatcher:

private final TextWatcher mTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}

@Override
public void afterTextChanged(Editable str) { 
    //don't allow special characters
    if (str.length() > 0) {
        StringBuilder validatedHandleBuilder = new StringBuilder();
        for (char c : str.toCharArray()) {
            if (mProfileManager.isAlphaNumeric(c)) {
                validatedHandleBuilder.append(c);
            }
        }

        String validatedHandle = validatedHandleBuilder.toString();
        if (!str.equals(validatedHandle)) {
            int cursorPosition = mTextInputEditText.getSelectionEnd() - 1;
            mTextInputEditText.setText(validatedHandle);
            int newCursorPosition = cursorPosition <     mTextInputEditText.length() ? cursorPosition : mTextInputEditText.length();
            mTextInputEditText.setSelection(newCursorPosition);
        }
    }
}   
};

I understand the IndexOutOfBoundsException but I am unable to pinpoint the line of code where I could handle for this exception.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Anil Gorthy
  • 869
  • 3
  • 13
  • 30
  • Open Debugger I guess? – Gurwinder Singh Dec 04 '16 at 18:02
  • Yes, I used a debugger to identify that it is calling `setSpan()` of `SpannableStringBuilder.java` but I cannot nail down at which point in my code the exception occurs. I suspect it has to do w/ cursor position since the length is being calculated there. – Anil Gorthy Dec 04 '16 at 18:08
  • put some breakpoints and execute step by step. - http://www.vogella.com/tutorials/EclipseDebugging/article.html – Gurwinder Singh Dec 04 '16 at 18:11
  • I am also facing this type issue if you have any solution then share with me. – Kishan Donga May 30 '18 at 04:45
  • I've seen this issue in Crash reports but am unable to reproduce it. Seems it may be limited to Android 7.1.1 Can you confirm? And what device if possible? – aaronvargas Mar 11 '19 at 19:56

0 Answers0