Mauricio
Mauricio

Reputation: 859

IndexOutOfBoundsException setSpan (34 ... 48) ends beyond length 34

I have a crash report indicating an IndexOutOfBoundsException but I have no idea what caused it since there isn't anything from my code in the stack. This is the stack:

Fatal Exception: java.lang.IndexOutOfBoundsException: setSpan (34 ... 48) ends beyond length 34
       at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1265)
       at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:684)
       at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:677)
       at android.widget.Editor$SuggestionsPopupWindow.updateSuggestions(Editor.java:3980)
       at android.widget.Editor$SuggestionsPopupWindow.show(Editor.java:3833)
       at android.widget.Editor.replace(Editor.java:435)
       at android.widget.Editor$3.run(Editor.java:2359)
       at android.os.Handler.handleCallback(Handler.java:751)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:154)
       at android.app.ActivityThread.main(ActivityThread.java:6776)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)

The only thing I've found similar to my problem is this but the guy wasn't able to solve it.

How can I know what caused it so I can look for a solution?

EDIT

Ok so I found in multiple parts of my large code this function being used:

public static Spannable spannabletext(String message, int start, int end, String font_family, Typeface font) {

    Spannable spannable = new SpannableString(Html.fromHtml(message));
    spannable.setSpan(new CustomTypefaceSpan(font_family, font),
            start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);

    return spannable;

}

So I'm guessing at some point this is where the problem comes from. But since I don't want to spend hours and hours trying to guess which text and in what language (my app is in 8 different languages) was this crash generated I'm going to wait till it happens again but this time I added some Crashlytics lines so I can get more info from the crash report. So my spannabletext function is like this now:

public static Spannable spannabletext(String message, int start, int end, String font_family, Typeface font, Activity activity) {

    Crashlytics.setString("activity", activity.getLocalClassName());
    Crashlytics.setString("message", message);
    Crashlytics.setInt("start", start);
    Crashlytics.setInt("end", end);

    Spannable spannable = new SpannableString(Html.fromHtml(message));
    spannable.setSpan(new CustomTypefaceSpan(font_family, font),
            start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);

    return spannable;

}

I'll keep you posted..

Upvotes: 5

Views: 4042

Answers (2)

Fedor
Fedor

Reputation: 43412

I am able to reproduce this crash with exactly the same stacktrace on my device. Can confirm it's really a framework issue and doesn't depend on your code. I can reproduce it in any app on my device including system apps like Messages, etc.

So what can we do:

  1. Ignore it. Rare case actually, hard to reproduce, not much users should be affected. Apparently google thinks the same. Because in my crash reports I can see this issue on Android 8,9,10,11,12,13 and even the latest 14. Still not fixed.
  2. Disable dictionary-based word suggestions with inputType="textNoSuggestions". It fixes the problem. But users would not be happy with this solution.
  3. Wait for a fix from Google. Here's a ticket https://issuetracker.google.com/issues/314288203. There's upvote button if you're concerned about this crash.

Upvotes: 0

Sylvester Yao
Sylvester Yao

Reputation: 295

The error is not caused by your code. See the stack: at android.widget.Editor$SuggestionsPopupWindow.updateSuggestions(Editor.java:3980) that occurred when long-clicking the text to show the PopWindow. So it's a framework error, not yours.

This answer is better: EditText crashing on clearing text

Problem is "updateSuggestions" try to setSpan after you change text in EditText. So you need make text change after editor modify EditText. Just put your code in view.post

mEditText.post {
   mEditText.setText("your text")
}

Upvotes: 1

Related Questions