从 Textview 中选择文本时出错 (java.lang.IndexOutOfBoundsException: setSpan (-1 ... -1) 在 0 之前开始)

2022-09-03 07:37:09

我正在尝试在文本视图上添加复制粘贴功能。当我尝试复制它时,我已添加在代码和xml中,效果很好,但是当它指向文本视图的第一个位置时,并且我们尝试选择文本时,它会抛出错误。我该如何解决?请帮帮我。registerForContextMenu(detailedText);android:textIsSelectable="true"

XML

  <TextView
            android:id="@+id/datailtext"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textIsSelectable="true"
            android:layout_below="@+id/EMPTY"
            android:text="@string/detailed_text"      
            />

错误

    04-02 16:54:03.367: E/AndroidRuntime(10977): FATAL EXCEPTION: main
    04-02 16:54:03.367: E/AndroidRuntime(10977): Process: com.example.app, PID: 10977
    04-02 16:54:03.367: E/AndroidRuntime(10977): java.lang.IndexOutOfBoundsException: setSpan (-1 ... -1) starts before 0
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.text.SpannableStringInternal.checkRange(SpannableStringInternal.java:355)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.text.SpannableStringInternal.setSpan(SpannableStringInternal.java:77)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.text.SpannableString.setSpan(SpannableString.java:46)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.text.Selection.setSelection(Selection.java:76)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.widget.Editor$SelectionEndHandleView.updateSelection(Editor.java:3479)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.widget.Editor$HandleView.positionAtCursorOffset(Editor.java:3167)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.widget.Editor$SelectionEndHandleView.updatePosition(Editor.java:3494)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.widget.Editor$HandleView.onTouchEvent(Editor.java:3260)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.View.dispatchTouchEvent(View.java:7690)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.View.dispatchPointerEvent(View.java:7870)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3919)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3808)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3406)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3456)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3425)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3510)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3433)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3567)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3406)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3456)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3425)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3433)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3406)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5520)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5500)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5471)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5594)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:182)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:174)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:5573)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:5613)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.Choreographer.doFrame(Choreographer.java:530)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.os.Handler.handleCallback(Handler.java:733)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.os.Handler.dispatchMessage(Handler.java:95)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.os.Looper.loop(Looper.java:137)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at android.app.ActivityThread.main(ActivityThread.java:4998)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at java.lang.reflect.Method.invokeNative(Native Method)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at java.lang.reflect.Method.invoke(Method.java:515)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
    04-02 16:54:03.367: E/AndroidRuntime(10977):    at dalvik.system.NativeStart.main(Native Method)

更新代码

好吧,我想在文本视图Android 2.3上实现复制粘贴功能,到目前为止我已经在代码中完成了,我在下面详细介绍了。在 android 2.3 中实现有什么更好的建议吗?请帮帮我。

  detailedText.setOnLongClickListener(new OnLongClickListener() {

        @SuppressLint("NewApi")
        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub

            Log.d("LOG", "Detail text long pressed");

            int startIndex = detailedText.getSelectionStart();
            int endIndex = detailedText.getSelectionEnd();

            Log.d("LOG","startIndex "+ startIndex + "endIndex  "  + endIndex);///here get index -1 ,-1 for startIndex and endIndex in less than android 4.4 i dont know why?


            String YouExtracted = stringYouExtracted.substring(startIndex,endIndex);

            int sdk = android.os.Build.VERSION.SDK_INT;
            if (sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
                android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                clipboard.setText(detailedText.getText().toString());
            } else {
                android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                android.content.ClipData clip = android.content.ClipData
                        .newPlainText("COPYTEXT", detailedText.getText().toString());
                clipboard.setPrimaryClip(clip);
            }
            return true;
        }
    });

    setupSocialNetworkingLinks();
}

/*@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    //user has long pressed your TextView
    menu.add(0, v.getId(), 0, "COPYTEXT");

    //cast the received View to TextView so that you can get its text
    TextView yourTextView = (TextView) detailedText;

    int startIndex = detailedText.getSelectionStart();
    int endIndex = detailedText.getSelectionEnd();
    String  YouExtracted = detailedText.getText().toString();

    //place your TextView's text in clipboard
    if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
        android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setText(YouExtracted);
    } else {
        android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        android.content.ClipData clip = android.content.ClipData.newPlainText("COPYING", YouExtracted);
        clipboard.setPrimaryClip(clip);
    }
}
*/

答案 1

我在文本视图中选择文本时遇到了同样的问题。它是 eventuate,因为 TextView 使用 SpannableString,但静态方法 Selected.getSelectionStart(CharSequence text) 如果文本不是 Spanned 的实例,则返回 -1。我解析它(以及Android 6 https://code.google.com/p/android/issues/detail?id=191430)ovveride dispatchTouchEvent,如下所示:

public class HackyTextView extends TextView {

    ...

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

答案 2

这有点晚了,但是你使用的是自定义的LinkMovementMethod吗?我发现了相同的异常,但是一旦我注释了setCustomMovementMethod,异常就会消失。

还不知道为什么会发生这种情况。