user3622613
user3622613

Reputation: 21

java.lang.IndexOutOfBoundsException: charAt: -1 < 0 in SpannableStringBuilder

I have an EditText with Clickable Spannable String.

But if the content contains a Spannable String and I tap on it or move the cursor in it, I get an IndexOutOfBoundsException.

Here is my error log.

05-22 12:28:01.413: E/AndroidRuntime(5482): java.lang.IndexOutOfBoundsException: charAt: -1 < 0
05-22 12:28:01.413: E/AndroidRuntime(5482):     at android.text.SpannableStringBuilder.charAt(SpannableStringBuilder.java:112)
05-22 12:28:01.413: E/AndroidRuntime(5482):     at android.text.Selection.setSelection(Selection.java:81)
05-22 12:28:01.413: E/AndroidRuntime(5482):     at android.text.Selection.setSelection(Selection.java:115)
05-22 12:28:01.413: E/AndroidRuntime(5482):     at android.widget.Editor$InsertionHandleView.updateSelection(Editor.java:3697)
05-22 12:28:01.413: E/AndroidRuntime(5482):     at android.widget.Editor$HandleView.positionAtCursorOffset(Editor.java:3417)
05-22 12:28:01.413: E/AndroidRuntime(5482):     at android.widget.Editor$HandleView.updatePosition(Editor.java:3444)
05-22 12:28:01.413: E/AndroidRuntime(5482):     at android.widget.Editor$PositionListener.onPreDraw(Editor.java:2285)
05-22 12:28:01.413: E/AndroidRuntime(5482):     at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:707)
05-22 12:28:01.413: E/AndroidRuntime(5482):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1936)
05-22 12:28:01.413: E/AndroidRuntime(5482):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1105)
05-22 12:28:01.413: E/AndroidRuntime(5482):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4462)
05-22 12:28:01.413: E/AndroidRuntime(5482):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
05-22 12:28:01.413: E/AndroidRuntime(5482):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
05-22 12:28:01.413: E/AndroidRuntime(5482):     at android.view.Choreographer.doFrame(Choreographer.java:525)
05-22 12:28:01.413: E/AndroidRuntime(5482):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
05-22 12:28:01.413: E/AndroidRuntime(5482):     at android.os.Handler.handleCallback(Handler.java:615)
05-22 12:28:01.413: E/AndroidRuntime(5482):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-22 12:28:01.413: E/AndroidRuntime(5482):     at android.os.Looper.loop(Looper.java:137)
05-22 12:28:01.413: E/AndroidRuntime(5482):     at android.app.ActivityThread.main(ActivityThread.java:4895)
05-22 12:28:01.413: E/AndroidRuntime(5482):     at java.lang.reflect.Method.invokeNative(Native Method)
05-22 12:28:01.413: E/AndroidRuntime(5482):     at java.lang.reflect.Method.invoke(Method.java:511)
05-22 12:28:01.413: E/AndroidRuntime(5482):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
05-22 12:28:01.413: E/AndroidRuntime(5482):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
05-22 12:28:01.413: E/AndroidRuntime(5482):     at dalvik.system.NativeStart.main(Native Method)```

Upvotes: 0

Views: 1649

Answers (2)

Chupik
Chupik

Reputation: 1050

I had a similar problem, looked like it was caused by LinkMovementMethod, used for my EditText with ClickableSpan.

In my case issue reproduced only with editText.setMovementMethod(LinkMovementMethod.getInstance());

So, to handle clicks on ClickableSpan without LinkMovementMethod - add custom onClickListener, something like this:

editText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Editable editable = editText.getText();
                int pos = editText.getSelectionStart();
                ClickableSpan[] link = editable.getSpans(pos,pos, ClickableSpan.class);
                if (link.length != 0) {
                    link[0].onClick(v);
                }
            }
        });

Upvotes: 1

Keerthivasan
Keerthivasan

Reputation: 12880

Index values start from 0. You are trying to access the character at index -1 (Which is an Illegal index) That's why you get the exception. May be you have to fix the loop which gets out of the valid index, while traversing the characters.

Please check out IndexOutOfBoundsException API

Upvotes: 1

Related Questions