赞
踩
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来适应处理。
上述处理方法仅为最小范围修改,如果心大的小伙伴,可以增加整个界面的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; }
由于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;
这个时候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]);
}
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。