Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when selecting text from Textview (java.lang.IndexOutOfBoundsException: setSpan (-1 ... -1) starts before 0)

I am trying add copy paste functionality on text view.I have added in code registerForContextMenu(detailedText); and also android:textIsSelectable="true" in xml.When I am trying to copy it works well,but when it pointed on first position of the text view,and than we try to select the text it throws error.which i shown below. how can i solve it ? please help me.

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"      
            />

Error

    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)

Update Code

well, I want implement copy paste functionality on text view Android 2.3,I have done so far in code,I detailed below.Any better suggestion for implementation in android 2.3?Please help me.

  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);
    }
}
*/
like image 282
Mayur Raval Avatar asked Apr 02 '14 11:04

Mayur Raval


2 Answers

I had the same problem with selecting text in TextView. It's eventuate, because TextView use SpannableString, but static method Selected.getSelectionStart(CharSequence text) return -1 if text not instance of Spanned. I resolve it (and https://code.google.com/p/android/issues/detail?id=191430 for Android 6 ) ovveride dispatchTouchEvent like this:

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);
    }
}
like image 180

It's a bit late, but are you using a custom LinkMovementMethod? I discovered the same Exception, but as soon as I comment the setCustomMovementMethod out, the Exception disappears.

Don't know why this happens yet.

like image 4
freg Avatar answered Nov 02 '22 00:11

freg