Lately we've been experiencing a lot of crashes in our application due to IndexOutOfBoundsException being thrown by Android internally.
Fatal Exception: java.lang.IndexOutOfBoundsException: setSpan (-1 ... -1) starts before 0
at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1331)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:685)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:677)
at android.view.accessibility.AccessibilityNodeInfo.replaceClickableSpan(AccessibilityNodeInfo.java:2925)
at android.view.accessibility.AccessibilityNodeInfo.setText(AccessibilityNodeInfo.java:2889)
at android.widget.TextView.onInitializeAccessibilityNodeInfoInternal(TextView.java:12028)
at android.view.View.onInitializeAccessibilityNodeInfo(View.java:8925)
at android.view.View.createAccessibilityNodeInfoInternal(View.java:8884)
at android.view.View.createAccessibilityNodeInfo(View.java:8869)
at android.view.AccessibilityInteractionController.populateAccessibilityNodeInfoForView(AccessibilityInteractionController.java:396)
at android.view.AccessibilityInteractionController.findAccessibilityNodeInfoByAccessibilityIdUiThread(AccessibilityInteractionController.java:358)
at android.view.AccessibilityInteractionController.access$500(AccessibilityInteractionController.java:74)
at android.view.AccessibilityInteractionController$PrivateHandler.handleMessage(AccessibilityInteractionController.java:1547)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:210)
at android.os.Looper.loop(Looper.java:299)
at android.app.ActivityThread.main(ActivityThread.java:8302)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1037)
have find answer in stack overflow Android SpannableStringBuilder IndexOutOfBoundsException but not find Any answer
I have try to reproduce but no luck, have one try that if device has talkback on and app is crash but not sure that all user has activate this talkback
I would be happy to hear any ideas what the problem could be :)
Considering whether you have used
SpannableString
andNoCopySpan
in your project. This error occurs when users enable the accessibility feature. You can trace the source code from the provided clues below.android.view.accessibility.AccessibilityNodeInfo#setText
android.view.accessibility.AccessibilityNodeInfo#replaceClickableSpan
SpannableStringBuilder
However, if you don't use
NoCopySpan
, it may lead to memory leaks. My personal solution is to abandon their usage and refactor the code in that part using alternative components.If you understand Chinese, you can refer to a post for more detailed information.