开发者

java.lang.IndexOutOfBoundsException: getChars (7 ... 0) has end before start

开发者 https://www.devze.com 2022-12-19 04:06 出处:网络
My users are sending unhandled exceptions to me via http://code.google.com/p/android-remote-stacktrace/

My users are sending unhandled exceptions to me via http://code.google.com/p/android-remote-stacktrace/

I am getting the following but have no idea what it means.

java.lang.IndexOutOfBoundsException: getChars (7 ... 0) has end before start
   at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:935)
   at android.text.SpannableStringBuilder.getChars(SpannableStringBuilder.java:847)
   at android.text.TextUtils.getChars(TextUtils.java:69)
   at android.text.SpannableStringBuilder.<init>(SpannableStringBuilder.java:59)
   at android.text.SpannableStringBuilder.subSequence(SpannableStringBuilder.java:839)
   at android.widget.TextView.extractTextInternal(TextView.java:4541)
   at android.widget.TextView.reportExtractedText(TextView.java:4580)
   at android.widget.TextView.finishBatchEdit(TextView.java:4723)
   at android.widget.TextView.endBatchEdit(TextView.java:4705)
   at com.android.internal.widget.EditableInputConnection.endBatchEdit(EditableInputConnection.java:54)
   at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:586)
   at android.view.开发者_StackOverflow社区inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:174)
   at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:120)
   at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:231)
   at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:57)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:4338)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
   at dalvik.system.NativeStart.main(Native Method)


It's a known bug in the Android framework. Here's a link to the issue.


if android couldnt set the selection you do that using cursor position... i resolved the issue by putting this exactly before the crashing line

editor.setSelection(editor.getText().length(), editor.getText().length());


For anyone still struggling with this issue, placing the following code in your Activity's onResume will solve it:

textEntry.setSelection(textEntry.getText().length(), textEntry.getText().length());


I fixed it with a custom textEntry. Changed onSelectionChanged and put there the code from loopj!

My code:

@Override
protected void onSelectionChanged(int selStart, int selEnd) {
    if (selStart >= 0) {
        super.onSelectionChanged(selStart, selEnd);
    } else {
        setSelection(getText().length());
    }
}


Its Late but yesterday i figure out this problem. The Problem is in your case is your end is before start like 7....0 which is wrong if you want to highlight a text you start should be less then your end. Check out posted example its perfectly working.

The Answer is for those Who still has this issue.

 String searchText = "Your search String";
        String qr_code = "Your String";

        int length = searchText.length();
        if (length > 0) {
            //color your text here
            int index = qr_code.indexOf(searchText);
            SpannableString sb = new SpannableString(qr_code);
            ForegroundColorSpan fcs = new ForegroundColorSpan(getResources().getColor(R.color.colorAccent));
            sb.setSpan(fcs, index, (index+length), Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
            holder.textViewShortCode.setText(sb);
        } else {
            textViewShortCode.setText(Html.fromHtml(qr_code));
        }


I had the same issue and fixed it. I got that app crash error in two places.

Below "searchText" is AutoCompleteTextView(it is a kind of EditText type)

  1. I got the error in this line searchText.ClearFocus();. i fixed the issue adding a line before this line as below,

     searchText.SetSelection(searchText.Text.Length, searchText.Text.Length);
     searchText.ClearFocus();
     
  2. I got the same error inside my existing event. I fixed doing a similar thing as the above 1 solution, inside the event.

    public void OnLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
         searchText.DropDownAnchor = Resource.Id.appbar;
         searchText.DropDownWidth = ViewGroup.LayoutParams.MatchParent;
         .......
         searchText.SetSelection(searchText.Text.Length, searchText.Text.Length);
    }
     
0

精彩评论

暂无评论...
验证码 换一张
取 消