我收到arrayoutofboundsexception错误。当我尝试格式化要显示的草稿文本消息时。这只是一个简单的文本消息应用程序,我试图设置文本大小和颜色。当我评论这两行时,我的应用程序没有崩溃,但没有按照我的意愿进行格式化。
错误发生在:
buf.setSpan(new TextAppearanceSpan(getContext(), size, color), before,
buf.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
以下是整个代码:
private CharSequence formatMessage(ConversationListItemData ch) {
final int size = android.R.style.TextAppearance_Small;
final int color = android.R.attr.textColorSecondary;
String from = ch.getFrom();
SpannableStringBuilder buf = new SpannableStringBuilder(from);
if (ch.getMessageCount() > 1) {
buf.append(" (" + ch.getMessageCount() + ") ");
}
int before = buf.length();
if (ch.hasDraft()) {
buf.append(" ");
buf.append(getContext().getResources().getString(R.string.has_draft));
buf.setSpan(new TextAppearanceSpan(getContext(), size, color), before,
buf.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
buf.setSpan(new ForegroundColorSpan(
getContext().getResources().getColor(R.drawable.text_color_red)),
before, buf.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
}
// Unread messages are shown in bold
if (!ch.isRead()) {
buf.setSpan(STYLE_BOLD, 0, buf.length(),
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
}
return buf;
}
答案 0 :(得分:1)
我明白了。使用ForegroundColorSpan时,不能使用具有3个参数的TextAppearanceSpan构造函数。我通过删除颜色变量参数来更改下面的行:
buf.setSpan(new TextAppearanceSpan(getContext(), size, color), before,
buf.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
到
buf.setSpan(new TextAppearanceSpan(getContext(), size), before,
buf.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);