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

前端之家收集整理的这篇文章主要介绍了从Textview中选择文本时出错(java.lang.IndexOutOfBoundsException:setSpan(-1 … -1)在0之前启动)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在文本视图中添加复制粘贴功能.我已经添加代码registerForContextMenu(detailedText);还有 android:textIsSelectable =“true”在xml.When我试图复制它工作得很好,但当它指向文本视图的第一个位置,而不是我们尝试选择文本,它会抛出错误,我显示如下.我该怎么解决呢?请帮帮我.

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(),"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);
    }
}
*/

解决方法

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

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

原文链接:https://www.f2er.com/java/123230.html

猜你在找的Java相关文章