17

I'm getting an IndexOutOfBoundsException on Samsung Galaxy S5 and Note 3 and 4. It doesn't reference my code. Has anyone encountered this? I haven't been able to find anything on here.

It appears to be occasionally happening when try to long-click on the EditText to paste something in.

Edit:

I'm using a simple EditText field (not in ListView or Spinner). There's a hint of around 28 characters. I'm toggling the focus via clearFocus a few times and I'm using a setOnEditorActionListener and setOnFocusChangeListener that control attaching a fragment to the Activity.

Edit #2:

I've been able to successfully reproduce it by trying to long press on the EditText to try to paste something. It only happens when there is text already in the EditText and I long press to the RIGHT of the text, not on it. Also, the EditText must not have focus.

Any possible solution by creating a custom EditText and overriding some methods?

java.lang.IndexOutOfBoundsException: setSpan (11 ... 11) ends beyond length 0
   at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1024)
   at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:594)
   at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:590)
   at android.text.Selection.setSelection(Selection.java:116)
   at android.text.Selection.setSelection(Selection.java:127)
   at android.widget.Editor.performLongClick(Editor.java:1008)
   at android.widget.TextView.performLongClick(TextView.java:10637)
   at android.view.View$CheckForLongPress.run(View.java:19482)
   at android.os.Handler.handleCallback(Handler.java:733)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:146)
   at android.app.ActivityThread.main(ActivityThread.java:5678)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
   at dalvik.system.NativeStart.main(Native Method)

Code:

editText.setOnEditorActionListener(new OnEditorActionListener() {

        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            String searchText = v.getText().toString();

            FragmentSearchList fragmentSearchList = (FragmentSearchList) getChildFragmentManager().findFragmentByTag(FRAGMENT_SEARCH_LIST_TAG);

            if(fragmentSearchList != null){
                fragmentSearchList.executeSearch(searchText);
            }

            return true;
        }

    });

editText.setOnFocusChangeListener(new OnFocusChangeListener(){

        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            if(hasFocus){

                FragmentManager fragmentManager = getChildFragmentManager();

                Fragment fragment = fragmentManager.findFragmentByTag(FRAGMENT_SEARCH_LIST_TAG);

                if(fragment == null){

                    editText.setText(null);

                    FragmentSearchList fragmentSearchList = FragmentSearchList.newInstance();

                    FragmentTransaction fragmentSearchListTransaction = fragmentManager.beginTransaction();
                    fragmentSearchListTransaction.add(R.id.viewGroupFragmentSearchListContainer, fragmentSearchList, FRAGMENT_SEARCH_LIST_TAG);
                    fragmentSearchListTransaction.addToBackStack(null);
                    fragmentSearchListTransaction.commit();

                }
            }
            else{

                if(!isRemovingOrPartOfRemovalChain()){
                    editText.setText(mAreaName);
                    getChildFragmentManager().popBackStack();
                }

            }
        }

    });


public boolean isRemovingOrPartOfRemovalChain(){

    if(isRemoving()){
        return true;
    }

    Fragment fragment = this.getParentFragment();

    if(fragment != null){
        if(((MainFragment)fragment).isRemovingOrPartOfRemovalChain()){
            return true;
        }
        else{
            return false;
        }
    }

    else{
        return(this.getActivity().isFinishing());
    }

}
matiash
  • 54,791
  • 16
  • 125
  • 154
ono
  • 2,984
  • 9
  • 43
  • 85
  • are you using listview or spinner?;you must post your code – said Dec 12 '14 at 16:09
  • No it's just a plain EditText. Check my edit – ono Dec 12 '14 at 16:23
  • 1
    WHAT IS THE CODE IN "setOnEditorActionListener"and onlongclicklistner – said Dec 12 '14 at 16:40
  • Please post more code in order for people to debug. – Azae B. Garcia Dec 12 '14 at 16:41
  • 3
    @ono Could be a timing issue. On gaining focus, you call `editText.setText(null);`. Try commenting out this statement, or delaying it using a Handler. – Vikram Dec 18 '14 at 16:15
  • you may try this 'editText.setText("");' alternate for 'editText.setText(null);' – Elango Dec 22 '14 at 06:49
  • @Elango `editText.setText("")` won't make a difference since the `null` argument is replaced by `""` itself. Take a look at the overridden `TextView#setText(CharSequence, BufferType, boolean, int)`. – Vikram Dec 22 '14 at 08:55
  • So commenting out the `editText.setText(null)` removes the crash, although it doesn't clear the current text. What's also interesting on other devices where it doesn't crash, the logcat gives me `E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length`. So it looks like it is an Android problem and not just Samsung. – ono Dec 22 '14 at 16:36
  • @ono The "SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length" sometimes occur depending on the keyboard you're using (SwiftKey in particular is a big offender in this regard) even when doing nothing in particular. It might be related, but not necessarily. – matiash Dec 23 '14 at 00:34
  • @one It's likely that using `Handler.post()` as @Vikram suggested will enable you to clear the `EditText` and avoid the exception. – matiash Dec 23 '14 at 00:36
  • Do you have more than one thread? If so, you could consider a `synchronize` around `OnEditorActionListener` and `OnFocusChangeListener` – StuartLC Dec 25 '14 at 15:26
  • @matiash @ono There might be another way. To avoid the exception, you need to stop the long-press action from kicking in. So, before calling `editText.setText(null)`, call `editText.cancelLongPress()`. The side effect of this will be that you won't see the `PASTE` action popup. To see the `PASTE` action, user will have to lift their finger and try long-pressing again. – Vikram Dec 25 '14 at 16:02
  • I'm ok with just removing the `editText.setText(null)`. I think the UX is acceptable. – ono Dec 25 '14 at 16:47

0 Answers0