java.lang.IndexOutOfBoundsException: setSpan (-1 ... -1) 在 Android 8.0 的 0 之前开始
Posted
技术标签:
【中文标题】java.lang.IndexOutOfBoundsException: setSpan (-1 ... -1) 在 Android 8.0 的 0 之前开始【英文标题】:java.lang.IndexOutOfBoundsException: setSpan (-1 ... -1) starts before 0 at Android 8.0 【发布时间】:2019-01-10 07:34:02 【问题描述】:问题 自从升级到 android 8.0 后,我收到许多崩溃报告,指出在我的代码之外发生的 IndexOutOfBoundsException。但与 the other question
不同崩溃报告 这似乎是Android本身的一个错误。但我想它是在我使用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)
在源码中,AccessibilityNodeInfo.java 和方法 setText(),可能有问题。但我不知道。我需要帮助。谢谢
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());
【问题讨论】:
我找到了原因,当用户在Android 8.0上打开Talkback时,可能会出现这个崩溃 【参考方案1】:尽量用字符串代替字符序列,请确保文本值不为空
【讨论】:
【参考方案2】:正如@chenjian chen 所说,如果在设置->辅助功能中启用了对讲,则可能会发生这种情况。我通过为导致此错误的视图添加 android:importantForAccessibility="no"
来解决此问题。
就我而言,我制作的 customView 是崩溃的原因。所以在构造函数中添加这一行修复问题:
setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
:
【讨论】:
以上是关于java.lang.IndexOutOfBoundsException: setSpan (-1 ... -1) 在 Android 8.0 的 0 之前开始的主要内容,如果未能解决你的问题,请参考以下文章