当前位置:   article > 正文

Android TextView IndexOutOfBoundsException: setSpan (-1 ... -1) starts before 0

Android TextView IndexOutOfBoundsException: setSpan (-1 ... -1) starts before 0

java.lang.IndexOutOfBoundsException: setSpan (-1 … -1) starts before 0
at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1330)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:684)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:676)
at android.view.accessibility.AccessibilityNodeInfo.replaceClickableSpan(AccessibilityNodeInfo.java:2906)
at android.view.accessibility.AccessibilityNodeInfo.setText(AccessibilityNodeInfo.java:2870)
at android.widget.TextView.onInitializeAccessibilityNodeInfoInternal(TextView.java:11800)
at android.view.View.onInitializeAccessibilityNodeInfo(View.java:8672)
at android.view.View.createAccessibilityNodeInfoInternal(View.java:8631)
at android.view.View.createAccessibilityNodeInfo(View.java:8616)
at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfRealNode(AccessibilityInteractionController.java:1271)
at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfRealNode(AccessibilityInteractionController.java:1294)
at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfRealNode(AccessibilityInteractionController.java:1294)
at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchAccessibilityNodeInfos(AccessibilityInteractionController.java:1096)
at android.view.AccessibilityInteractionController.findAccessibilityNodeInfoByAccessibilityIdUiThread(AccessibilityInteractionController.java:342)
at android.view.AccessibilityInteractionController.access$400(AccessibilityInteractionController.java:75)
at android.view.AccessibilityInteractionController$PrivateHandler.handleMessage(AccessibilityInteractionController.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:8060)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)

在设置了clickspan的textview出现了如上问题
该问题呢,本身属于系统bug,但系统不是那么容易升级的。基本要先靠app来适应处理。

解决方法(任意)

  1. 布局文件中增加TextView的属性
    android:importantForAccessibility=“noHideDescendants”
  2. 代码中设置textview.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);

上述处理方法仅为最小范围修改,如果心大的小伙伴,可以增加整个界面的importantForAccessibility属性。

测试与说明

此问题要开启系统的无障碍 - 视觉- talkback 后可以进行验证。正常测试是不会开启无障碍的,同时崩溃堆栈中也不包含项目痕迹,多数情况下可能任由这个问题的发生。但从统计数量来说,这个问题的发生还比较多。很汗颜。同时捕获到的信息来看,Android 10和Android 11的用户。
在这里插入图片描述

根据堆栈信息,跟了一下源码,从TextView:onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) 到AccessibilityNodeInfo:setText(CharSequence text)

//level 33
 private CharSequence replaceClickableSpan(CharSequence text) {
        ClickableSpan[] clickableSpans =
                ((Spanned) text).getSpans(0, text.length(), ClickableSpan.class);
        Spannable spannable = new SpannableStringBuilder(text);
        if (clickableSpans.length == 0) {
            return text;
        }
        for (int i = 0; i < clickableSpans.length; i++) {
            ClickableSpan span = clickableSpans[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);
            if (spanToReplaceStart < 0) {
                continue;
            }
            spannable.removeSpan(span);
            ClickableSpan replacementSpan = (span instanceof URLSpan)
                    ? new AccessibilityURLSpan((URLSpan) span)
                    : new AccessibilityClickableSpan(span.getId());
            spannable.setSpan(replacementSpan, spanToReplaceStart, spanToReplaceEnd,
                    spanToReplaceFlags);
        }
        return spannable;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

由于AccessibliltyNodeInfo 不是将text类型转换成SpannableString,而是新建一个 SpannableStringBuilder对象:
Spannable spannable = new SpannableStringBuilder(text);

spannable的Spans数组没有初始化是null,只有在第一次setSpan(obj what,start,end,…)的时候才能将what也就是 ClickableSpan和start end 存入到Spans数组内

// setSpan(obj what,start,end,...)
        mSpans = GrowingArrayUtils.append(mSpans, mSpanCount, what);
        mSpanStarts = GrowingArrayUtils.append(mSpanStarts, mSpanCount, start);
        mSpanEnds = GrowingArrayUtils.append(mSpanEnds, mSpanCount, end);
        mSpanFlags = GrowingArrayUtils.append(mSpanFlags, mSpanCount, flags);
        mSpanOrder = GrowingArrayUtils.append(mSpanOrder, mSpanCount, mSpanInsertCount);
        invalidateIndex(mSpanCount);
        mSpanCount++;
        mSpanInsertCount++;
.....
 if (send) {
            restoreInvariants();
            sendSpanAdded(what, nstart, nend);
        }
//在restoreInvariants()中:
....
 for (int i = mLowWaterMark; i < mSpanCount; i++) {
            Integer existing = mIndexOfSpan.get(mSpans[i]);
            if (existing == null || existing != i) {
                mIndexOfSpan.put(mSpans[i], i);
            }
        }
        mLowWaterMark = Integer.MAX_VALUE;

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

这个时候mIndexOfSpan内才会存储mSpans的一系列参数,否则mIndexOfSpan就为null,所以在 SpannableStringBuilder中getSpanStart 会return-1,进而在checkSpan中报错

public int getSpanStart(Object what) {
        if (mIndexOfSpan == null) return -1;
        Integer i = mIndexOfSpan.get(what);
        return i == null ? -1 : resolveGap(mSpanStarts[i]);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/654423
推荐阅读
相关标签
  

闽ICP备14008679号