Milan Cerovský
Milan Cerovský

Reputation: 21

Android SpannableStringBuilder IndexOutOfBoundsException

Lately we've been experiencing a lot of crashes in our application due to IndexOutOfBoundsException being thrown by Android internally.

The issue happens on screens with static layout that consists of TextViews, Checkboxes and Buttons on API levels 26 -> 31 and all manufacturers.

We have not been able to reproduce it any way, yet we tried fixing it 'blindly' by setting android:importantForAccessibility="no" on TextViews.

I would be happy to hear any ideas what the problem could be :)

Fatal Exception: java.lang.IndexOutOfBoundsException: setSpan (-1 ... -1) starts before 0
       at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1318)
       at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:684)
       at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:676)
       at android.view.accessibility.AccessibilityNodeInfo.setText(AccessibilityNodeInfo.java:2474)
       at android.widget.TextView.onInitializeAccessibilityNodeInfoInternal(TextView.java:10642)
       at android.view.View.onInitializeAccessibilityNodeInfo(View.java:7345)
       at android.view.View.createAccessibilityNodeInfoInternal(View.java:7304)
       at android.view.View.createAccessibilityNodeInfo(View.java:7289)
       at android.view.accessibility.AccessibilityRecord.setSource(AccessibilityRecord.java:146)
       at android.view.accessibility.AccessibilityRecord.setSource(AccessibilityRecord.java:119)
       at android.view.View.onInitializeAccessibilityEventInternal(View.java:7241)
       at android.widget.TextView.onInitializeAccessibilityEventInternal(TextView.java:10623)
       at android.view.View.onInitializeAccessibilityEvent(View.java:7229)
       at android.view.View.sendAccessibilityEventUncheckedInternal(View.java:7091)
       at android.view.View.sendAccessibilityEventUnchecked(View.java:7076)
       at android.view.View$SendViewStateChangedAccessibilityEvent.run(View.java:26260)
       at android.os.Handler.handleCallback(Handler.java:808)
       at android.os.Handler.dispatchMessage(Handler.java:101)
       at android.os.Looper.loop(Looper.java:166)
       at android.app.ActivityThread.main(ActivityThread.java:7529)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

Upvotes: 1

Views: 1555

Answers (1)

Milan Cerovský
Milan Cerovský

Reputation: 21

I fixed the issue.

The problem in our case was we used a ClickableSpan which also extended the NoCopySpan interface. Setting android:importantForAccessibility="no" was only a partial fix because this flag can get ignored (u also might need to avoid this flag if u want to support accessibility).

So the solution was to remove the interface and always set the text of given TextView to null in onDestroyView method of given fragment.

Upvotes: 1

Related Questions