By simply setting a TextViews
text to a string defined in code (that never can be null
) I sometimes get an ArrayIndexOutOfBoundsException
, this only happens in my live app, I never had this issue on any test device yet... And it seems to happen sometimes only, the user tells me it happens once and the next time everything works again. Any ideas what could be the cause here? For me, it looks like this crash should never ever happen...
Code
I do following:
fun updateFilterInfo(showing: Int, total: Int) {
binding?.tvFilterLvl1?.text = "$showing / $total" // <= THIS line creates the crash
}
Crash
And I get following crash every now and then (very rarely):
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at android.text.SpannableStringBuilder.getSpansRec(SpannableStringBuilder.java:973)
at android.text.SpannableStringBuilder.getSpans(SpannableStringBuilder.java:866)
at android.text.SpannableStringBuilder.getSpans(SpannableStringBuilder.java:836)
at android.text.SpannableStringBuilder.sendSpanAdded(SpannableStringBuilder.java:1268)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:773)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:672)
at android.widget.TextView.setText(TextView.java:5981)
at android.widget.TextView.setText(TextView.java:5849)
at android.widget.TextView.setText(TextView.java:5806)
... above line of my code ...
Versions/Background
I'm using android x, newest stable versions and have this problem... Also I use kotlin 1.3.21
and Gradle plugin 3.3.2
- all this are stable and mostly the newest versions. Additionally I made sure the user does not use a custom ROM, so it can't be this either.
XML/Theme
The affected view is very simple, like the following:
<TextView
android:id="@+id/tvFilterLvl1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:padding="4dp" />
No custom styling here, no custom fonts inside my project (although a system-wide custom font can never be excluded). My base app theme does not define a custom TextView
style and extends Theme.MaterialComponents.Light.DarkActionBar.Bridge
. So neither the XML nor the theme has any special TextView
handling...
Edit 1 - I extracted the value to a local variable
fun updateFilterInfo(showing: Int, total: Int) {
val text = "$showing / $total"
binding?.tvFilterLvl1?.text = text
}
Still I got a crash like following:
java.lang.NullPointerException: Attempt to invoke interface method 'void android.text.SpanWatcher.onSpanAdded(android.text.Spannable, java.lang.Object, int, int)' on a null object reference
at android.text.SpannableStringBuilder.sendSpanAdded(SpannableStringBuilder.java:1228)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:767)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:677)
at android.text.DynamicLayout.<init>(DynamicLayout.java:187)
at android.widget.TextView.makeSingleLayout(TextView.java:6907)
at android.widget.TextView.makeNewLayout(TextView.java:6805)
at android.widget.TextView.checkForRelayout(TextView.java:7341)
at android.widget.TextView.setText(TextView.java:4482)
at android.widget.TextView.setText(TextView.java:4339)
at android.widget.TextView.setText(TextView.java:4314)
.... the line above: binding?.tvFilterLvl1?.text = text