Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IndexOutOfBoundsException for setSpan - outside my application code

Tags:

java

android

I recently published an update to my Android app and have been getting a ton of crash reports, with the following stack. I'm at a loss since there's absolutely no code of mine in the stack, so I'm not sure how to tackle this problem. Has anyone ever seen this error? Anything I can do to address this or debug it?

java.lang.IndexOutOfBoundsException: setSpan (0 ... 11) ends beyond length 0
   at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1016)
   at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:592)
   at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:588)
   at android.widget.TextView.setSpan_internal(TextView.java:8610)
   at android.widget.Editor$SuggestionsPopupWindow.onItemClick(Editor.java:2902)
   at android.widget.AdapterView.performItemClick(AdapterView.java:298)
   at android.widget.AbsListView.performItemClick(AbsListView.java:1128)
   at android.widget.AbsListView$PerformClick.run(AbsListView.java:2812)
   at android.widget.AbsListView$1.run(AbsListView.java:3571)
   at android.os.Handler.handleCallback(Handler.java:725)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:153)
   at android.app.ActivityThread.main(ActivityThread.java:5299)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
   at dalvik.system.NativeStart.main(NativeStart.java)

Edit: From the stack, it looks like it's going through a ListView -> itemClick -> then trying to do something with a TextView. I do have multiple clickable listViews with textViews in them... Is there a property I can set on the textViews to stop this from happening?

like image 597
zedix Avatar asked Nov 15 '14 17:11

zedix


2 Answers

i think you have white spaces in your string you have like eleven white space string but no character so actual/trimmed length is zero, this will cause error to set selection. you can check myString.trim().lenght() > 0 before setting your selection position.

like image 164
nosaiba darwish Avatar answered Nov 14 '22 23:11

nosaiba darwish


When suggestion is used to correct the wrong word, Editor#replaceWithSuggestion is called, and then TextView#setSpan_internal and setCursorPosition_internal are called, but maxLength is not judged here, so it crashes.

Thanks Ring. The following solved my issue. I extended EditText and override following methods,

protected void setSpan_internal(Object span, int start, int end, intflags) {
        final int textLength = getText().length();
        ((Editable) getText()).setSpan(span, start, Math.min(end, textLength), flags);
    }

protected void setCursorPosition_internal(int start, int end) {
    final int textLength = getText().length();
    Selection.setSelection(((Editable) getText()), Math.min(start, textLength), Math.min(end, textLength));
}

From: https://issuetracker.google.com/issues/36944935#comment5

like image 34
user7229569 Avatar answered Nov 14 '22 23:11

user7229569