IndexOutOfBoundsException on Samsung devices

醉酒当歌 提交于 2019-12-06 19:44:35

问题


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());
    }

}

来源:https://stackoverflow.com/questions/27447117/indexoutofboundsexception-on-samsung-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!