坑--TextView同时使用maxLines=1和ellipsize时偶发崩溃

今天发现项目的报错记录中排名第一的是ArrayIndexOutOfBoundsException错误,影响用户84,发生次数129。

报错堆栈如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1 android.text.StaticLayout.getLineTop(StaticLayout.java:878)
2 android.widget.TextView.getExtendedPaddingTop(TextView.java:1966)
3 android.widget.TextView.bringTextIntoView(TextView.java:7291)
4 android.widget.TextView.onPreDraw(TextView.java:5107)
5 android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
6 android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2348)
7 android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1292)
8 android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6598)
9 android.view.Choreographer$CallbackRecord.run(Choreographer.java:800)
10 android.view.Choreographer.doCallbacks(Choreographer.java:603)
11 android.view.Choreographer.doFrame(Choreographer.java:572)
12 android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:786)
13 android.os.Handler.handleCallback(Handler.java:815)
14 android.os.Handler.dispatchMessage(Handler.java:104)
15 android.os.Looper.loop(Looper.java:194)
16 android.app.ActivityThread.main(ActivityThread.java:5682)
17 java.lang.reflect.Method.invoke(Native Method)
18 java.lang.reflect.Method.invoke(Method.java:372)

从错误堆栈里并没有找到特别有用的信息。

于是去看了下报错的设备信息:

| 机型设备 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 TextViews that are using ellipsize attrs.

来源:https://stackoverflow.com/questions/45487427/crashlytics-reporting-multiples-issues-for-textview-makesinglelayout

Author

calvinche

Posted on

2018-12-26

Licensed under

CC BY-NC-SA 4.0

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×