Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting crash due to StringIndexOutOfBoundsException inside RecyclerView #54

Open
kasim1011 opened this issue May 28, 2020 · 10 comments
Open

Comments

@kasim1011
Copy link

kasim1011 commented May 28, 2020

I'm using ReadMoreTextView in RecyclerView.
Getting crash due to StringIndexOutOfBoundsException so many times inside RecyclerView.

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.waspito, PID: 17585
    java.lang.StringIndexOutOfBoundsException: length=35; index=241
        at java.lang.String.getChars(String.java:788)
        at android.text.TextUtils.getChars(TextUtils.java:148)
        at android.text.SpannableStringBuilder.<init>(SpannableStringBuilder.java:68)
        at com.borjabravo.readmoretextview.ReadMoreTextView.updateCollapsedText(ReadMoreTextView.java:137)
        at com.borjabravo.readmoretextview.ReadMoreTextView.getTrimmedText(ReadMoreTextView.java:114)
        at com.borjabravo.readmoretextview.ReadMoreTextView.getDisplayableText(ReadMoreTextView.java:90)
        at com.borjabravo.readmoretextview.ReadMoreTextView.setText(ReadMoreTextView.java:84)
        at com.borjabravo.readmoretextview.ReadMoreTextView.access$200(ReadMoreTextView.java:33)
        at com.borjabravo.readmoretextview.ReadMoreTextView$1.onGlobalLayout(ReadMoreTextView.java:206)
        at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:1056)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2629)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1722)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7605)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1029)
        at android.view.Choreographer.doCallbacks(Choreographer.java:852)
        at android.view.Choreographer.doFrame(Choreographer.java:787)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1014)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7397)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)
@kasim1011 kasim1011 changed the title Getting crash due to StringIndexOutOfBoundsException Getting crash due to StringIndexOutOfBoundsException inside RecyclerView May 28, 2020
@eggham0518
Copy link

same

@Narinc
Copy link

Narinc commented Sep 23, 2020

I think not only in recyclerview

@Qdafengzi
Copy link

have solve it ?

@fukemy
Copy link

fukemy commented Mar 25, 2021

hmm i think need. to change to other lib

@ShareemGelitoTeofilo
Copy link

I've tried debugging this and I found that this part right here is causing it.

On this method of ReadMoreTextView.java

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

When you set the mode to TRIM_MODE_LINES, that case will execute and there are times that the value of trimEndIndex is negative so the if statement below it will execute and when trimLength is greater than the actual text length, this will cause the SpannableStringBuilder to throw and index out of bounds error.

ps. the trimLength have a default value of 240

@ShareemGelitoTeofilo
Copy link

I found out that one of the reasons for this is old data is still there, because of the recycling mechanism of recyclerview. What I mean is the old text is still there and it was used to calculate other variables within the ReadMoreTextView to initialize it, so by the time that the new text is passed into it will used the precomputed values from the old data. In this case the length of the old text is used to initialize the SpannableStringBuilder which caused the index out of bounds.

I added a setting to recyclerview to disable the recycling mechanism to avoid this issue. Altho it solved it, but it beats the purpose of the recyclerview and it obviously affected the performance. Have to find another way for this. Maybe put a handler inside the ReadMoreTextView to update the data effectively or wait for the recyclerview to finally load the new data first, but don't know how yet. Hope this will help others to figure out this problem once and for all.

@fukemy
Copy link

fukemy commented Jun 19, 2021

i do not have much time to solve the library problem, so i using this lib : https://github.com/devendroid/ReadMoreOption

@ShareemGelitoTeofilo
Copy link

I have a possible solution. I'll work on this.

@fukemy
Copy link

fukemy commented Jun 19, 2021

ok buddy, if u want to make it easy
i think it will better using this layout and toggle hide/show instead of readmore

LinearLayout vertical
LinearLayout horizontal
[TRIM_TEXT] -Read more btn-
LinearLayout horizontal
[FULL_TEXT]
LinearLayout vertical

@baltekgajda
Copy link

Is anyone still working on this lib? No pull requests, no commits, is there maybe an updated repo with this issues fixed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants