码迷,mamicode.com
首页 > 其他好文 > 详细

BaseAdapter的ArrayIndexOutOfBoundsException

时间:2015-06-03 19:03:51      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

最近写一个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

代码如下:

 

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



 

我的GlobalConstant.java中定义了type:

 

  1. public static final int TYPE_IN                         = 3;    // sms type: inbox  
  2.       
  3.     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,就解决了该问题:

 

 

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

BaseAdapter的ArrayIndexOutOfBoundsException

标签:

原文地址:http://www.cnblogs.com/exmyth/p/4549800.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!