Android 错误 :TextView中属性ellipsize的 值为start、middle可能会出现错误

来源:互联网 发布:程序员代码面试指 pdf 编辑:程序博客网 时间:2024/05/22 00:06

错误摘要如下:

Java.lang.ArrayIndexOutOfBoundsException: length=*; index=-1

at android.text.StaticLayout.calculateEllipsis(StaticLayout.java:*)
at android.text.StaticLayout.out(StaticLayout.java:*)
at android.text.StaticLayout.generate(StaticLayout.java:*)
at android.text.StaticLayout.<init>(StaticLayout.java:*)
at android.widget.TextView.makeSingleLayout(TextView.java:*)

at android.widget.TextView.makeNewLayout(TextView.java:*)


*为行数不一定。 反正是数组越界,本来以为是自己的代码问题,来回,反复测试,Debug发现不是自己代码错误,是TextView的bug。


当TextView 属性ellipsize的值设为start、middle的时候可能会出现这种错误。


解决方法:

如果TextView的值太长一定要省略,省略尾部可以避免这个问题,即ellipsize的值设为end。


或者(下面这个方法未尝试,可以自行尝试一下):

摘自:stackoverflow

They should have at least added this to Lint! :S You can use a custom textview to fix this.(你可以使用一个自定义的TextView去修复这个bug)

public class MyTextView extends TextView {    public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init();    }    public MyTextView(Context context, AttributeSet attrs) {        super(context, attrs);        init();    }    public MyTextView(Context context) {        super(context);        init();    }    private void init() {        // this is to overcome the calculateEllipsis bug in some versions of android, I spotted it on 4.4.4 and 4.4.3        // see https://code.google.com/p/android/issues/detail?id=33868        if (Build.VERSION.SDK_INT >= 16) {            if (getMaxLines() == 1) {                setSingleLine(true);            }        }    }}

reference MyTextView in your xml instead of the normal textview.(在你的xml文件中引用MyTextView替换一般的TextView)


有梯子的可以通过下面这个链接看Bug报告:

https://code.google.com/p/android/issues/detail?id=33868

0 0
原创粉丝点击