由于 Spannable TextView 导致的 java.lang.IllegalArgumentException [英] java.lang.IllegalArgumentException due to Spannable TextView

查看:18
本文介绍了由于 Spannable TextView 导致的 java.lang.IllegalArgumentException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Google Play 管理中心收到了这个崩溃报告,我自己从未体验过,也无法重现.

I received this crash report in my Google Play Console which I myself never experience and unable to reproduce.

java.lang.IllegalArgumentException: 
  at android.text.method.WordIterator.checkOffsetIsValid (WordIterator.java:380)
  at android.text.method.WordIterator.isBoundary (WordIterator.java:101)
  at android.widget.Editor$SelectionStartHandleView.positionAtCursorOffset (Editor.java:4300)
  at android.widget.Editor$HandleView.updatePosition (Editor.java:3736)
  at android.widget.Editor$PositionListener.onPreDraw (Editor.java:2513)
  at android.view.ViewTreeObserver.dispatchOnPreDraw (ViewTreeObserver.java:944)
  at android.view.ViewRootImpl.performTraversals (ViewRootImpl.java:2417)
  at android.view.ViewRootImpl.doTraversal (ViewRootImpl.java:1321)
  at android.view.ViewRootImpl$TraversalRunnable.run (ViewRootImpl.java:6708)
  at android.view.Choreographer$CallbackRecord.run (Choreographer.java:894)
  at android.view.Choreographer.doCallbacks (Choreographer.java:696)
  at android.view.Choreographer.doFrame (Choreographer.java:631)
  at android.view.Choreographer$FrameDisplayEventReceiver.run (Choreographer.java:880)
  at android.os.Handler.handleCallback (Handler.java:815)
  at android.os.Handler.dispatchMessage (Handler.java:104)
  at android.os.Looper.loop (Looper.java:207)
  at android.app.ActivityThread.main (ActivityThread.java:5728)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:789)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:679)

崩溃报告没有提到导致崩溃的类.我最好的猜测是跟随自定义 TextView.

The crash report didn't mention which class is causing the crash. My best guess is the follow custom TextView.

private void customTextView(TextView view) {
        SpannableStringBuilder spanTxt = new SpannableStringBuilder(
                Html.fromHtml(definition[0]));
        spanTxt.append("\n\nExample");
        spanTxt.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View view) {
                showAlert();
            }
        }, spanTxt.length() -"Example".length(), spanTxt.length(), 0);
        view.setMovementMethod(LinkMovementMethod.getInstance());
        view.setText(spanTxt, TextView.BufferType.SPANNABLE);
    }

我的自定义 TextView 有问题吗?

Is there any problem with my custom TextView?

推荐答案

由于这里并未共享您的所有代码,因此我不清楚实际问题出在哪里.该异常似乎是由 Choreographer 中可选 TextView 中的光标位置超出范围引起的.从 WordIterator.java 源代码我看到:

Since not all of your code is shared here, it is not obvious to me where the actual issue is. The exception seems to be caused by the cursor position within a selectable TextView in a Choreographer being out of bounds. From the WordIterator.java source code I see:

private void checkOffsetIsValid(int offset) {
   if (!(mStart <= offset && offset <= mEnd)) {
        throw new IllegalArgumentException("Invalid offset: " + (offset) +
           ". Valid range is [" + mStart + ", " + mEnd + "]");
   }
}

我个人没有遇到过这个问题,但在调用 setText 时,Android 可能会清除 TextView 选择,正如这篇文章中所述(有一些解决方法)在 Android TextView 中选择文本时出现 IllegalArgumentException

I personally have not run into this problem but there could be an issue with Android clearing the TextView selection when calling setText as is explained (with some workarounds) in this post IllegalArgumentException while selecting text in Android TextView

祝你好运!

这篇关于由于 Spannable TextView 导致的 java.lang.IllegalArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆