BaseAdapter的ArrayIndexOutOfBoundsException

最近写一个listView中多个listItem布局时,convertView缓存及使用,类似微信的聊天界面的listView,报了一个异常:

11-25 15:51:49.076: E/InputEventReceiver(22971): Exception dispatching input event.
11-25 15:51:49.128: E/AndroidRuntime(22971): FATAL EXCEPTION: main
11-25 15:51:49.128: E/AndroidRuntime(22971): java.lang.ArrayIndexOutOfBoundsException: length=2; index=3
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:6771)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5265)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3232)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.widget.AbsListView.onTouchEvent(AbsListView.java:3558)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.view.View.dispatchTouchEvent(View.java:7315)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2253)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1950)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2259)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1964)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2259)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1964)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2259)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1964)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2259)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1964)
11-25 15:51:49.128: E/AndroidRuntime(22971): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1970)
11-25 15:51:49.128: E/AndroidRuntime(22971): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1420)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.app.Activity.dispatchTouchEvent(Activity.java:2444)
11-25 15:51:49.128: E/AndroidRuntime(22971): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1918)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.view.View.dispatchPointerEvent(View.java:7506)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3680)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3605)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4875)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4835)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4991)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:171)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4959)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:5013)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.view.Choreographer.doCallbacks(Choreographer.java:579)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.view.Choreographer.doFrame(Choreographer.java:546)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.os.Handler.handleCallback(Handler.java:800)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.os.Handler.dispatchMessage(Handler.java:100)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.os.Looper.loop(Looper.java:194)
11-25 15:51:49.128: E/AndroidRuntime(22971): at android.app.ActivityThread.main(ActivityThread.java:5398)
11-25 15:51:49.128: E/AndroidRuntime(22971): at java.lang.reflect.Method.invokeNative(Native Method)
11-25 15:51:49.128: E/AndroidRuntime(22971): at java.lang.reflect.Method.invoke(Method.java:525)
11-25 15:51:49.128: E/AndroidRuntime(22971): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-25 15:51:49.128: E/AndroidRuntime(22971): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
11-25 15:51:49.128: E/AndroidRuntime(22971): at dalvik.system.NativeStart.main(Native Method)
11-25 15:51:49.182: E/AppErrorDialog(590): Failed to get ILowStorageHandle instance

代码如下:

[java] view plaincopyprint?

  1. class FavoriteSmsListAdapter extends BaseAdapter {
  2. private Context mContext;
  3. private LayoutInflater mInflater;
  4. private ArrayList<FavoriteSmsModel> mDataList;
  5. public FavoriteSmsListAdapter(Context context) {
  6. super();
  7. this.mContext = context;
  8. this.mInflater = LayoutInflater.from(context);
  9. }
  10. private void setUpData(ArrayList<FavoriteSmsModel> dataList) {
  11. this.mDataList = dataList;
  12. }
  13. @Override
  14. public int getCount() {
  15. return mDataList.size();
  16. }
  17. @Override
  18. public Object getItem(int position) {
  19. return mDataList.get(position);
  20. }
  21. @Override
  22. public long getItemId(int position) {
  23. return position;
  24. }
  25. @Override
  26. public int getItemViewType(int position) {
  27. return mDataList.get(position).getType();
  28. }
  29. @Override
  30. public int getViewTypeCount() {
  31. return 2;
  32. }
  33. @Override
  34. public View getView(final int position, View convertView, ViewGroup parent) {
  35. ViewHolderIn holderIn = null;
  36. ViewHolderOut holderOut = null;
  37. int type = getItemViewType(position);
  38. if(convertView == null)
  39. {
  40. switch(type){
  41. case GlobalConstant.TYPE_IN:
  42. convertView = mInflater.inflate(R.layout.favorite_sms_list_item_recv, parent, false);
  43. holderIn = new ViewHolderIn();
  44. holderIn.mFavSmsViewParent = (LinearLayout) convertView.findViewById(R.id.fav_sms_view_parent);
  45. holderIn.mBodyView = (TextView) convertView.findViewById(R.id.body_view);
  46. holderIn.mAddressView = (TextView) convertView.findViewById(R.id.address_view);
  47. holderIn.mDateView = (TextView) convertView.findViewById(R.id.date_view);
  48. convertView.setTag(holderIn);
  49. break;
  50. case GlobalConstant.TYPE_OUT:
  51. convertView = mInflater.inflate(R.layout.favorite_sms_list_item_send, parent, false);
  52. holderOut = new ViewHolderOut();
  53. holderOut.mFavSmsViewParent = (LinearLayout) convertView.findViewById(R.id.fav_sms_view_parent);
  54. holderOut.mBodyView = (TextView) convertView.findViewById(R.id.body_view);
  55. holderOut.mAddressView = (TextView) convertView.findViewById(R.id.address_view);
  56. holderOut.mDateView = (TextView) convertView.findViewById(R.id.date_view);
  57. convertView.setTag(holderOut);
  58. break;
  59. }
  60. }else{
  61. switch(type){
  62. case GlobalConstant.TYPE_IN:
  63. holderIn = (ViewHolderIn)convertView.getTag();
  64. break;
  65. case GlobalConstant.TYPE_OUT:
  66. holderOut = (ViewHolderOut)convertView.getTag();
  67. break;
  68. }
  69. }
  70. FavoriteSmsListItem listItem = (FavoriteSmsListItem) convertView;
  71. switch(type){
  72. case GlobalConstant.TYPE_IN:
  73. listItem.bind(mContext, mDataList.get(position), holderIn.mFavSmsViewParent, holderIn.mBodyView, holderIn.mAddressView, holderIn.mDateView);
  74. break;
  75. case GlobalConstant.TYPE_OUT:
  76. listItem.bind(mContext, mDataList.get(position), holderOut.mFavSmsViewParent, holderOut.mBodyView, holderOut.mAddressView, holderOut.mDateView);
  77. break;
  78. }
  79. return convertView;
  80. }
  81. }
  82. private class ViewHolderIn {
  83. LinearLayout mFavSmsViewParent;
  84. TextView mBodyView;
  85. TextView mAddressView;
  86. TextView mDateView;
  87. }
  88. private class ViewHolderOut {
  89. LinearLayout mFavSmsViewParent;
  90. TextView mBodyView;
  91. TextView mAddressView;
  92. TextView mDateView;
  93. }

我的GlobalConstant.java中定义了type:

[java] view plaincopyprint?

  1. public static final int TYPE_IN                         = 3;    // sms type: inbox
  2. public static final int TYPE_OUT                        = 4;    // sim of other type

琢磨了半天,我的getViewTypeCount()返回的是2,getItemViewType获取的是type值,同样是2个,为什么会报这个错误,在网上搜索到了一个关键信息:

stackoverflow上看到的回答如下:

The item view type you are returning from getItemViewType() is >= getViewTypeCount().

确实是这个问题,我的type定义了2种,而且是从3开始。。。

讲GlobalConstant.java中的type值分别改为0,1,就解决了该问题:

[java] view plaincopyprint?

    1. public static final int TYPE_IN                         = 0;    // sms type: inbox
    2. public static final int TYPE_OUT                        = 1;    // sim of other type
时间: 2024-11-13 09:51:10

BaseAdapter的ArrayIndexOutOfBoundsException的相关文章

Android BaseAdapter加载多个不同的Item布局时出现UncaughtException in Thread main java.lang.ArrayIndexOutOfBoundsException: length=15; index=15

java.lang.ArrayIndexOutOfBoundsException: length=15; index=15 异常出现的场景:在做聊天界面时,需要插入表情,图片,文字,名片,还有几种较为复杂的布局.这时就需要用到BaseAdapter中的getViewTypeCount()和getItemViewType(int position) 方法了,在发送复杂界面时出现了这个异常. 令人抓狂的是这个异常居然是UncaughtException,根本无法判断哪一行出错了,刚开始的时候觉得一定

android BaseAdapter java.lang.ArrayIndexOutOfBoundsException!!!!!!!!!

最近碰到了个很奇葩的问题 同样的代码在两部不同手机呈现不同的状态 一部三星手机运行流畅没有问题 一部国产联想手机 运行报数组越界的错误 找了好久 将可能出错的地方定位在了 adapter 里面 可到低是哪里出现越界了呢?? 我是需要在一个listview里显示2个布局 需要在getItemViewType方法中返回两个数值 起初一个定义为1(CHAT_COME) 一个值为2(CHAT_TO)  ok 应该没有什么问题, 特别注意了 一下 这个方法的奇葩规定 就是必须要返回一个0! 可还是出问题了

BaseAdapter

BaseAdapter是为ListView.GridView适配数据的抽象类! public class MainAdapter extends BaseAdapter{ private LayoutInflater inflater; private Context context; public MainAdapter(Context context){ this.context=context; inflater=(LayoutInflater)context.getSystemServi

安卓学习第13课——BaseAdapter

BaseAdapter创建这么一个对象,需要些四个方法. int getCount(); Object getItem(int position); long getItemId(int position);View getView(int position, View convertView, ViewGroup parent);(1)列表中的项数(2)返回值的列表内容(3)获得postion处的列表项的ID(4)该列表项里的组件 package com.example.baseadapter

Android学习笔记-构建一个可复用的自定义BaseAdapter

转载自http://www.runoob.com/w3cnote/android-tutorial-customer-baseadapter.html   作者:coder-pig 本节引言: 如题,本节给大家带来的是构建一个可复用的自定义BaseAdapter,我们每每涉及到ListView GridView等其他的Adapter控件,都需要自己另外写一个BaseAdapter类,这样显得非常麻烦, 又比如,我们想在一个界面显示两个ListView的话,我们也是需要些两个BaseAdapter

Android BaseAdapter用法

BaseAdapter 适配器 BaseAdapter是一个抽象类,因此要写自已的适配器,段继承此类,并实现以下方法: @Overridepublic int getCount() { return 0; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Overridepublic View g

View(视图)——ListView之BaseAdapter

1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent

BaseAdapter封装 实现万能适配器

1.封装ViewHodler 1 package com.example.utils; 2 3 import android.content.Context; 4 import android.graphics.Bitmap; 5 import android.util.SparseArray; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9

BaseAdapter的getView方法中对setOnclickListener优化

在应用开发中凡是又列表式的界面我都采取使用ListView来呈现界面,并且需要一个BaseAdapter来加载数据进去,所以我们就需要重写getView方法,那么问题来了,在每一个Item都有一个按钮的话我们需要给按钮setOnclickListener,假设我们按一下代码来写的话: ActiveClick ac = new ActiveClick(); convertView.findViewById(R.id.main).setOnClickListener(ac); 通过打印我们可以看到l