坑--TextView同时使用maxLines=1和ellipsize时偶发崩溃
今天发现项目的报错记录中排名第一的是ArrayIndexOutOfBoundsException
错误,影响用户84,发生次数129。
报错堆栈如下:
1 | 1 android.text.StaticLayout.getLineTop(StaticLayout.java:878) |
从错误堆栈里并没有找到特别有用的信息。
于是去看了下报错的设备信息:
| 机型设备 Top 5 | 系统版本 Top 5 | | ——————— | ——————————- | | 华为 TIT-TL00 11.26% | Android 5.1,level 22 31.40% | | 三星 GT-I908 26.48% | Android 4.1.2,level 16 20.48% | | 荣耀 CUN AL00 6.14% | Android 4.0.4,level 15 10.92% | | 华为 TAG-TL00 5.46% | Android 7.0 8.19% | | HTC T327T 4.44% | Android 4.0.3,level 15 7.85% |
可谓是五花八门,看来也不是特定机型或者特定系统的问题。
从日志和设备信息都分析不出问题,只能靠google了,通过不断的搜索,渐渐发现类似的崩溃跟TextView的两个属性有关系,分别是:
- Android:maxLines
- Android:ellipsize
根据查询的信息,得出了下面的结论:
如果同时设置了ellipsize和maxLine=1,就会在某些机型上产生崩溃(不是必先),报ArrayIndexOutOfBoundsException的异常。解决办法就是将maxLine=1换成已经废弃的singleLine=true。这是Android系统的一个bug,目前还没有解决。
下面查询到的相关记录:
If you try to use maxLines=1
with ellipsize
, you shall get the following lint error (refer to this discussion)
Combining ellipsize and maxLines=1 can lead to crashes on some devices. Earlier versions of lint recommended replacing singleLine=true with maxLines=1 but that should not be done when using ellipsize
来源:https://code.luasoftware.com/tutorials/android/android-textview-singleline-with-ellipsis/
问题 Crash when using ellipsize=”start” (Jelly Bean) 的回复
ri…@gmail.com ri...@gmail.com #3Jul 12, 2012 08:17AM
I do notice that changing android:lines=”1” to android:singleLine=”true” causes the crash not to happen.
来源:https://issuetracker.google.com/issues/36950033
I faced what I suspect is the same problem in my own app. For me, it was happening because I was using android:ellipsize="start"
without also using android:singleLine="true"
.
We had switched all of our android:singleLine="true"
attributes to the recommended android:maxLines="1"
, but it turns out that there’s a bug in how the system calculates the ellipsis that is triggered if singleLine
isn’t present.
So I believe you can solve this issue by simply adding android:singleLine="true"
to your TextView
s that are using ellipsize
attrs.
来源:https://stackoverflow.com/questions/45487427/crashlytics-reporting-multiples-issues-for-textview-makesinglelayout
坑--TextView同时使用maxLines=1和ellipsize时偶发崩溃
https://chewenkai.github.io/uncategorized/坑-textview同时使用maxlines-1和ellipsize时偶发崩溃/