tView中选择文本时发生IllegalArgumentExce

tView中选择文本时发生IllegalArgumentExce

本文介绍了在Android TextView中选择文本时发生IllegalArgumentException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在可选的Android TextView中取消选择文本时遇到崩溃.当我选择一个TextView并设置LinkMovementMethod时,会发生这种情况.

I encountered a crash while deselecting text in a selectable Android TextView. This happens when I make a TextView selectable and set LinkMovementMethod.

TextView中的IllegalArgumentException

似乎是Android内部的错误.

Seems it's a bug inside Android.

java.lang.IllegalArgumentException: Invalid offset: -1. Valid range is [0, 10562]
    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:4260)
    at android.widget.Editor$HandleView.updatePosition(Editor.java:3708)
    at android.widget.Editor$PositionListener.onPreDraw(Editor.java:2507)
    at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2055)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
    at android.view.Choreographer.doCallbacks(Choreographer.java:670)
    at android.view.Choreographer.doFrame(Choreographer.java:606)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

推荐答案

感谢mariotaku的回答.由于某种原因,实际上我不是我要在TextView中选择文本.我做了一些调整,仅在选择了某些字符后才触发变通方法.现在看来效果很好!

Thanks mariotaku for the answer. For some reason it wouldn't me me actually select the text in my TextView. I tweaked it a little to only trigger the work around when some characters are selected. It seems to be working great now!

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    // FIXME simple workaround to https://code.google.com/p/android/issues/detail?id=191430
    int startSelection = getSelectionStart();
    int endSelection = getSelectionEnd();
    if (startSelection != endSelection) {
        if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
            final CharSequence text = getText();
            setText(null);
            setText(text);
        }
    }
    return super.dispatchTouchEvent(event);
}

这篇关于在Android TextView中选择文本时发生IllegalArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 04:39