IndexOutOfBoundsException in Android 8.0 outside C

2019-03-14 23:53发布

问题:

The problem

Since the upgrade to Android 8.0 I get many crash reports stating an IndexOutOfBoundsException that occurs outside my code.

The crash report

There is no code in the android app responsible for the crash and it seems to be a bug in Android itself.

java.lang.IndexOutOfBoundsException: 
  at android.text.SpannableStringBuilder.checkRange (SpannableStringBuilder.java:1314)
  at android.text.SpannableStringBuilder.setSpan (SpannableStringBuilder.java:680)
  at android.text.SpannableStringBuilder.setSpan (SpannableStringBuilder.java:672)
  at android.view.accessibility.AccessibilityNodeInfo.setText (AccessibilityNodeInfo.java:2474)
  at android.widget.TextView.onInitializeAccessibilityNodeInfoInternal (TextView.java:10357)
  at android.view.View.onInitializeAccessibilityNodeInfo (View.java:7307)
  at android.view.View.createAccessibilityNodeInfoInternal (View.java:7266)
  at android.view.View.createAccessibilityNodeInfo (View.java:7251)
  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:7203)
  at android.widget.TextView.onInitializeAccessibilityEventInternal (TextView.java:10338)
  at android.view.View.onInitializeAccessibilityEvent (View.java:7191)
  at android.view.View.sendAccessibilityEventUncheckedInternal (View.java:7053)
  at android.view.View.sendAccessibilityEventUnchecked (View.java:7038)
  at android.view.View.sendAccessibilityEventInternal (View.java:7015)
  at android.widget.TextView.sendAccessibilityEventInternal (TextView.java:10725)
  at android.view.View.sendAccessibilityEvent (View.java:6982)
  at android.widget.TextView.onSelectionChanged (TextView.java:9269)
  at android.widget.TextView.spanChange (TextView.java:9505)
  at android.widget.TextView$ChangeWatcher.onSpanRemoved (TextView.java:11943)
  at android.text.SpannableStringInternal.sendSpanRemoved (SpannableStringInternal.java:408)
  at android.text.SpannableStringInternal.removeSpan (SpannableStringInternal.java:243)
  at android.text.SpannableString.removeSpan (SpannableString.java:50)
  at android.text.Selection.removeSelection (Selection.java:109)
  at android.text.method.LinkMovementMethod.onTakeFocus (LinkMovementMethod.java:239)
  at android.widget.Editor.onFocusChanged (Editor.java:1163)
  at android.widget.TextView.onFocusChanged (TextView.java:9586)
  at android.view.View.handleFocusGainInternal (View.java:6593)
  at android.view.View.requestFocusNoSearch (View.java:10823)
  at android.view.View.requestFocus (View.java:10802)
  at android.view.ViewGroup.onRequestFocusInDescendants (ViewGroup.java:3204)
  at android.view.ViewGroup.requestFocus (ViewGroup.java:3160)
  at android.view.ViewGroup.onRequestFocusInDescendants (ViewGroup.java:3204)
  at android.view.ViewGroup.requestFocus (ViewGroup.java:3160)
  at android.view.ViewGroup.onRequestFocusInDescendants (ViewGroup.java:3204)
  at android.view.ViewGroup.requestFocus (ViewGroup.java:3160)
  at android.view.ViewGroup.onRequestFocusInDescendants (ViewGroup.java:3204)
  at android.view.ViewGroup.requestFocus (ViewGroup.java:3160)
  at android.view.ViewGroup.onRequestFocusInDescendants (ViewGroup.java:3204)
  at android.view.ViewGroup.requestFocus (ViewGroup.java:3160)
  at android.view.ViewGroup.onRequestFocusInDescendants (ViewGroup.java:3204)
  at android.view.ViewGroup.requestFocus (ViewGroup.java:3160)
  at android.view.ViewGroup.onRequestFocusInDescendants (ViewGroup.java:3204)
  at android.view.ViewGroup.requestFocus (ViewGroup.java:3160)
  at android.view.ViewGroup.onRequestFocusInDescendants (ViewGroup.java:3204)
  at android.view.ViewGroup.requestFocus (ViewGroup.java:3160)
  at android.view.ViewGroup.onRequestFocusInDescendants (ViewGroup.java:3204)
  at android.view.ViewGroup.requestFocus (ViewGroup.java:3163)
  at android.view.View.requestFocus (View.java:10769)
  at android.view.View.requestFocus (View.java:10711)
  at android.view.ViewRootImpl.focusableViewAvailable (ViewRootImpl.java:3430)
  at android.view.View.setFlags (View.java:13277)
  at android.view.View.setVisibility (View.java:9378)
  at android.app.Activity.makeVisible (Activity.java:5412)
  at android.app.ActivityThread.handleResumeActivity (ActivityThread.java:3785)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2898)
  at android.app.ActivityThread.-wrap11 (Unknown Source)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1593)
  at android.os.Handler.dispatchMessage (Handler.java:105)
  at android.os.Looper.loop (Looper.java:164)
  at android.app.ActivityThread.main (ActivityThread.java:6541)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:240)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:767)

Possible cause

Since it happens on a TextView I think it has to do with these elements I use:

         <TextView
              android:id="@+id/txtAlso"
              android:layout_width="0dp"
              android:layout_weight="1"
              android:layout_height="wrap_content"
              android:textColor="#0000AA"
              android:padding="5dp"
              android:textIsSelectable="true"
              android:text="" />

Tried solution

To hopefully fix this bug I have rewritten the TextViews to the code below, removing the isSelectable tag and preventing the view to focus. Since I cannot reproduce the bug myself I hope to get more information soon. If anyone has information, insight or knows the solution reply or comment.

         <TextView
              android:id="@+id/txtAlso"
              android:layout_width="0dp"
              android:layout_weight="1"
              android:layout_height="wrap_content"
              android:textColor="#0000AA"
              android:padding="5dp"
              android:focusable="false" android:focusableInTouchMode="false"
              android:text="" />

回答1:

I had the same crash in my app. After alot of research and testing I manage to recreate it. The crash happend only on Oreo Android 8.0 devices and almost only on Samsung devices.

The IndexOutOfBounds exception was in my case triggered when clicking on a link created by Linkify.

Linkify.addLinks(text, path, null, matchFilter, transforFilter);

I removed Linkify and found another way to do what I needed without Linkify.