我总是在收到一些消息之后得到ArrayIndexOutOfBoundsException
,无论是从数据库接收还是从数据库加载。
它总是发生在同一个索引
java.lang.ArrayIndexOutOfBoundsException:length = 2;索引= 2
整个跟踪属于框架,我无法理解我哪里出错了。
片段的一部分:
@Override
public void chatCreated(Chat chat, boolean createdLocally) {
Log.v(TAG, ".chatCreated createdLocally:" + createdLocally);
chat.addMessageListener(new ChatMessageListener() {
@Override
public void processMessage(Chat chat, Message message) {
Log.v(TAG, "Message Received : " + message);
Log.v(TAG, "From:" + message.getFrom());
Log.v(TAG, "Type : " + message.getType());
Log.v(TAG, "Boody:" + message.getBody());
final Message msg = message;
Date date = new Date();
if (message.getBody() != null) {
final SqlMessage sqlMessage = new SqlMessage();
sqlMessage.body = message.getBody();
sqlMessage.time = new java.sql.Date(System.currentTimeMillis());
sqlMessage.type = SqlMessage.TYPE_RECEIVER;
MessagesDao.storeNew(mDatabase, sqlMessage);
Handler handler = new Handler(Looper.getMainLooper());
handler.post(() -> {
mChatMessageAdapter.add(sqlMessage);
Log.v(TAG, "msg inside handler : " + msg);
});
}
}
});
}
private class ChatMessageAdapter extends ArrayAdapter<SqlMessage> {
private LayoutInflater mInflater;
public ChatMessageAdapter(Context context) {
super(context, -1);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d(TAG, "getview pos:" + position);
View view = convertView;
if (view == null) {
int resource;
switch (getItemViewType(position)) {
case SqlMessage.TYPE_SENDER:
resource = R.layout.list_item_sender_msg;
break;
case SqlMessage.TYPE_RECEIVER:
resource = R.layout.list_item_receiver_msg;
break;
default:
throw new IllegalArgumentException();
}
view = mInflater.inflate(resource, parent, false);
}
TextView textView = (TextView) view.findViewById(R.id.message);
SqlMessage message = getItem(position);
String time = new SimpleDateFormat("h:mm a", Locale.US).format(message.time);
Spannable messageSpannable = new SpannableString(message.body + '\n' + time);
messageSpannable.setSpan(new ForegroundColorSpan(Color.WHITE), messageSpannable.length() - time.length(),
messageSpannable.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
messageSpannable.setSpan(new RelativeSizeSpan(0.8f), messageSpannable.length() - time.length(),
messageSpannable.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(messageSpannable);
return view;
}
@Override
public int getItemViewType(int position) {
return getItem(position).type;
}
@Override
public int getViewTypeCount() {
return 2;
}
}
迹:
05-21 10:14:32.561 9432-9432/genie.com.yourgenieapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: genie.com.yourgenieapp, PID: 9432
java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:6563)
at android.widget.ListView.layoutChildren(ListView.java:1632)
at android.widget.AbsListView.onLayout(AbsListView.java:2148)
at android.view.View.layout(View.java:15614)
at android.view.ViewGroup.layout(ViewGroup.java:4968)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
at android.view.View.layout(View.java:15614)
at android.view.ViewGroup.layout(ViewGroup.java:4968)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15614)
at android.view.ViewGroup.layout(ViewGroup.java:4968)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15614)
at android.view.ViewGroup.layout(ViewGroup.java:4968)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:493)
at android.view.View.layout(View.java:15614)
at android.view.ViewGroup.layout(ViewGroup.java:4968)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15614)
at android.view.ViewGroup.layout(ViewGroup.java:4968)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15614)
at android.view.ViewGroup.layout(ViewGroup.java:4968)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15614)
at android.view.ViewGroup.layout(ViewGroup.java:4968)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2102)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1859)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1077)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5884)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
答案 0 :(得分:6)
事实证明我的public int getItemViewType(int position)
是罪魁祸首。我在其他地方更改了代码并返回了索引1
和2
,而不是0
和1
。
这可能是一个小错误,但不正确的错误报告使这成为一个主要问题。当getItemViewType(int position)
返回的数字大于getViewTypeCount() - 1
感谢 TacticalJoke (#android-dev
上的IRC缺口)修复此错误。