Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IndexOutOfBoundsException: setSpan (-1 ... -1) starts before 0 at Android 8.0

The problem Since the upgrade to Android 8.0 I get many crash reports stating an IndexOutOfBoundsException that occurs outside my code.But different with the other question

The crash report it seems to be a bug in Android itself.But I guess it appeared when I used the link in the content from TextView.

Fatal Exception: java.lang.IndexOutOfBoundsException: setSpan (-1 ... -1) starts before 0
       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:2510)
       at android.widget.TextView.onInitializeAccessibilityNodeInfoInternal(TextView.java:12047)
       at android.view.View.onInitializeAccessibilityNodeInfo(View.java:7958)
       at android.view.View.createAccessibilityNodeInfoInternal(View.java:7917)
       at android.view.View.createAccessibilityNodeInfo(View.java:7902)
       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:7854)
       at android.widget.TextView.onInitializeAccessibilityEventInternal(TextView.java:12028)
       at android.view.View.onInitializeAccessibilityEvent(View.java:7842)
       at android.view.View.sendAccessibilityEventUncheckedInternal(View.java:7704)
       at android.view.View.sendAccessibilityEventUnchecked(View.java:7689)
       at android.view.View.sendAccessibilityEventInternal(View.java:7666)
       at android.widget.TextView.sendAccessibilityEventInternal(TextView.java:12418)
       at android.view.View.sendAccessibilityEvent(View.java:7633)
       at android.widget.TextView.onSelectionChanged(TextView.java:10559)
       at android.widget.TextView.spanChange(TextView.java:10844)
       at android.widget.TextView$ChangeWatcher.onSpanAdded(TextView.java:14126)
       at android.text.SpannableStringInternal.sendSpanAdded(SpannableStringInternal.java:399)
       at android.text.SpannableStringInternal.setSpan(SpannableStringInternal.java:222)
       at android.text.SpannableStringInternal.setSpan(SpannableStringInternal.java:152)
       at android.text.SpannableString.setSpan(SpannableString.java:46)
       at android.text.Selection.setSelection(Selection.java:78)
       at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:218)
       at android.widget.TextView.onTouchEvent(TextView.java:11325)
       at android.view.View.dispatchTouchEvent(View.java:12539)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2786)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2786)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2786)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2786)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2786)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2786)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2786)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2786)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2786)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2786)
       at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:600)
       at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1871)
       at android.app.Activity.dispatchTouchEvent(Activity.java:3384)
       at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
       at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
       at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:562)
       at android.view.View.dispatchPointerEvent(View.java:12787)
       at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5625)
       at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5420)
       at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4913)
       at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4966)
       at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4932)
       at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5069)
       at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4940)
       at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5126)
       at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4913)
       at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4966)
       at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4932)
       at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4940)
       at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4913)
       at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7672)
       at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7612)
       at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7573)
       at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7783)
       at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:197)
       at android.os.MessageQueue.nativePollOnce(MessageQueue.java)
       at android.os.MessageQueue.next(MessageQueue.java:325)
       at android.os.Looper.loop(Looper.java:142)
       at android.app.ActivityThread.main(ActivityThread.java:6940)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

In the source, AccessibilityNodeInfo.java and method setText(),maybe something wrong.but I don't know.I need help.Thanks

 public void setText(CharSequence text) {
        enforceNotSealed();
        mOriginalText = text;
        // Replace any ClickableSpans in mText with placeholders
        if (text instanceof Spanned) {
            ClickableSpan[] spans =
                    ((Spanned) text).getSpans(0, text.length(), ClickableSpan.class);
            if (spans.length > 0) {
                Spannable spannable = new SpannableStringBuilder(text);
                for (int i = 0; i < spans.length; i++) {
                    ClickableSpan span = spans[i];
                    if ((span instanceof AccessibilityClickableSpan)
                            || (span instanceof AccessibilityURLSpan)) {
                        // We've already done enough
                        break;
                    }
                    int spanToReplaceStart = spannable.getSpanStart(span);
                    int spanToReplaceEnd = spannable.getSpanEnd(span);
                    int spanToReplaceFlags = spannable.getSpanFlags(span);
                    spannable.removeSpan(span);
                    ClickableSpan replacementSpan = (span instanceof URLSpan)
                            ? new AccessibilityURLSpan((URLSpan) span)
                            : new AccessibilityClickableSpan(span.getId());
                    spannable.setSpan(replacementSpan, spanToReplaceStart, spanToReplaceEnd,
                            spanToReplaceFlags);
                }
                mText = spannable;
                return;
            }
        }
        mText = (text == null) ? null : text.subSequence(0, text.length());
    }
like image 894
chenjian chen Avatar asked Aug 02 '18 13:08

chenjian chen


2 Answers

Try to use string instead of charsequence, and please make sure text value isn't empty

like image 174
Yasser Ajaj Avatar answered Nov 15 '22 14:11

Yasser Ajaj


as @chenjian chen mentioned it maybe occur if Talkback is enabled in Settings->Accessibility. I fix this by add android:importantForAccessibility="no" for views that cause this error. in my case, a customView that I make it is reason of crash. so add this line in constructor fixed problem: setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);:

like image 31
Hadi Ahmadi Avatar answered Nov 15 '22 13:11

Hadi Ahmadi