bravoborja/ReadMoreTextView

App crushes with StringIndexOutOfBoundsException

Closed this issue · 3 comments

This happens when text leght is shorter than field may fit without trimming and parameter app:trimMode="trimModeLine" is set.

For example
If I set attributes as following:
app:trimMode="trimModeLine"
app:trimLength="5"

and try to set some short text :
readmoretextview.setText("some text"); // lenght() = 9 which is shorter than 5 lines

It will crush.

Meanwhile attribute app:trimMode="trimModeLength" works correctly with any text lenghts

@droidcrib can you fix it and submit a pull request? I can't reproduce it in any of my devices. Saw similar crash reports but, not able to reproduce it.

naqi commented

@gnumilanix I think the problem is associated with using ReadMoreTextView in recycler view. When setting text to a view which is recycled lineEndIndex is not updated which triggers an out of bound exception at ReadMoreTextView.java:137

Me too

Process: com.atom.zeniius.dev, PID: 31592
   java.lang.StringIndexOutOfBoundsException: length=150; regionStart=0; regionLength=199
       at java.lang.String.startEndAndLength(String.java:504)
       at java.lang.String.getChars(String.java:815)
       at android.text.TextUtils.getChars(TextUtils.java:80)
       at android.text.SpannableStringBuilder.<init>(SpannableStringBuilder.java:64)
       at co.rockship.common.widgets.ReadMoreTextView.updateCollapsedText(ReadMoreTextView.java:131)
       at co.rockship.common.widgets.ReadMoreTextView.getTrimmedText(ReadMoreTextView.java:107)
       at co.rockship.common.widgets.ReadMoreTextView.getDisplayableText(ReadMoreTextView.java:83)
       at co.rockship.common.widgets.ReadMoreTextView.setText(ReadMoreTextView.java:77)
       at co.rockship.common.widgets.ReadMoreTextView.setText(ReadMoreTextView.java:90)
       at android.widget.TextView.setText(TextView.java:3900)

It link to this line


private CharSequence updateCollapsedText() {
        int trimEndIndex = text.length();
        switch (trimMode) {
            case TRIM_MODE_LINES:
                trimEndIndex = lineEndIndex - (ELLIPSIZE.length() + trimCollapsedText.length() + 1);
                if (trimEndIndex < 0) {
                    trimEndIndex = trimLength + 1;
                }
                break;
            case TRIM_MODE_LENGTH:
                trimEndIndex = trimLength + 1;
                break;
        }
        SpannableStringBuilder s = new SpannableStringBuilder(text, 0, trimEndIndex)
                .append(ELLIPSIZE)
                .append(trimCollapsedText);
        return addClickableSpan(s, trimCollapsedText);
    }