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

错误摘要如下:

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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值