很久以前做的表情输入及显示,用的系统的SpannableString,完成后的代码在其他版本的Android手机上没有问题,但是在在4.1和4.1.1的手机上显示时,有概率出现程序崩溃的问题。
下面是此问题摘出的异常信息:
9-17 16:38:27.429: E/AndroidRuntime(10425): FATAL EXCEPTION: main
09-17 16:38:27.429: E/AndroidRuntime(10425): java.lang.ArrayIndexOutOfBoundsException: length=116; index=125
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.text.MeasuredText.addStyleRun(MeasuredText.java:168)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.text.MeasuredText.addStyleRun(MeasuredText.java:204)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.text.StaticLayout.generate(StaticLayout.java:297)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.text.StaticLayout.(StaticLayout.java:156)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.text.StaticLayout.(StaticLayout.java:96)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.text.StaticLayout.(StaticLayout.java:75)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.TextView.makeSingleLayout(TextView.java:5942)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.TextView.makeNewLayout(TextView.java:5782)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.TextView.onMeasure(TextView.java:6139)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.view.View.measure(View.java:15264)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.view.View.measure(View.java:15264)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.view.View.measure(View.java:15264)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.ListView.setupChild(ListView.java:1893)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.ListView.makeAndAddView(ListView.java:1803)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.ListView.fillDown(ListView.java:681)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.ListView.fillFromTop(ListView.java:742)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.ListView.layoutChildren(ListView.java:1629)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.AbsListView.onLayout(AbsListView.java:2224)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.view.View.layout(View.java:13846)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.view.ViewGroup.layout(ViewGroup.java:4466)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.view.View.layout(View.java:13846)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.view.ViewGroup.layout(ViewGroup.java:4466)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.view.View.layout(View.java:13846)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.view.ViewGroup.layout(ViewGroup.java:4466)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.view.View.layout(View.java:13846)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.view.ViewGroup.layout(ViewGroup.java:4466)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.view.View.layout(View.java:13846)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.view.ViewGroup.layout(ViewGroup.java:4466)
09-17 16:38:27.429: E/AndroidRuntime(10425): at com.component.SinglePreviewContainer.onLayout(SinglePreviewContainer.java:124)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.view.View.layout(View.java:13846)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.view.ViewGroup.layout(ViewGroup.java:4466)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.view.View.layout(View.java:13846)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.view.ViewGroup.layout(ViewGroup.java:4466)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1638)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.LinearLayout.onLayout(LinearLayout.java:1422)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.view.View.layout(View.java:13846)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.view.ViewGroup.layout(ViewGroup.java:4466)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.view.View.layout(View.java:13846)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.view.ViewGroup.layout(ViewGroup.java:4466)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.view.View.layout(View.java:13846)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.view.ViewGroup.layout(ViewGroup.java:4466)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
09-17 16:38:27.429: E/AndroidRuntime(10425): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
原因:当TextView显示的表情恰好要被换行符截断的时候,会报异常(目前只在4.1和4.1.1上出现)
解决方式:
重写TextView 在onMeasure()中捕获异常
public class PatchedTextView extends TextView {
public PatchedTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public PatchedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public PatchedTextView(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
try{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}catch (ArrayIndexOutOfBoundsException e){
setText(getText().toString());
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
@Override
public void setGravity(int gravity){
try{
super.setGravity(gravity);
}catch (ArrayIndexOutOfBoundsException e){
setText(getText().toString());
super.setGravity(gravity);
}
}
@Override
public void setText(CharSequence text, BufferType type) {
try{
super.setText(text, type);
}catch (ArrayIndexOutOfBoundsException e){
setText(text.toString());
}
}
}
崩溃的特征:
当Android正在渲染一个非常长的一行内容到屏幕的时候,它需要去指出在哪里划线。但是如果下面的一些情况都满足了,就会出现一次崩溃:
1. 文本内容包括了MetricAffectingSpans (另:StyleSpan是它的一个子类).其他类型的Spans不会产生这个崩溃。
2.在一个此词的中间存在MetricAffectingSpans的内容开始或者结尾的词(例如:一个词的前一半是加粗,但是后一半不是加粗的)
3. 需要在一个word的非跨越的部分自动换行
4.代码运行在Android 4.1的手机上面。