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

代码如下:

class FavoriteSmsListAdapter extends BaseAdapter {
		private Context mContext;
		private LayoutInflater mInflater;
		private ArrayList<FavoriteSmsModel> mDataList;

		public FavoriteSmsListAdapter(Context context) {
			super();
			this.mContext = context;
			this.mInflater = LayoutInflater.from(context);
		}

		private void setUpData(ArrayList<FavoriteSmsModel> dataList) {
			this.mDataList = dataList;
		}

		@Override
		public int getCount() {
			return mDataList.size();
		}

		@Override
		public Object getItem(int position) {
			return mDataList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public int getItemViewType(int position) {
			return mDataList.get(position).getType();
		}
		
		@Override
		public int getViewTypeCount() {
			return 2;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolderIn holderIn = null;
			ViewHolderOut holderOut = null;
			int type = getItemViewType(position);
			
			if(convertView == null)
			{
				switch(type){
				case GlobalConstant.TYPE_IN:
					convertView = mInflater.inflate(R.layout.favorite_sms_list_item_recv, parent, false);
					holderIn = new ViewHolderIn();
					holderIn.mFavSmsViewParent = (LinearLayout) convertView.findViewById(R.id.fav_sms_view_parent);
					holderIn.mBodyView = (TextView) convertView.findViewById(R.id.body_view);
					holderIn.mAddressView = (TextView) convertView.findViewById(R.id.address_view);
					holderIn.mDateView = (TextView) convertView.findViewById(R.id.date_view);
					convertView.setTag(holderIn);
					break;
				case GlobalConstant.TYPE_OUT:
					convertView = mInflater.inflate(R.layout.favorite_sms_list_item_send, parent, false);
					holderOut = new ViewHolderOut();
					holderOut.mFavSmsViewParent = (LinearLayout) convertView.findViewById(R.id.fav_sms_view_parent);
					holderOut.mBodyView = (TextView) convertView.findViewById(R.id.body_view);
					holderOut.mAddressView = (TextView) convertView.findViewById(R.id.address_view);
					holderOut.mDateView = (TextView) convertView.findViewById(R.id.date_view);
					convertView.setTag(holderOut);
					break;
				}
			}else{
				switch(type){
				case GlobalConstant.TYPE_IN:
					holderIn = (ViewHolderIn)convertView.getTag();
					break;
				case GlobalConstant.TYPE_OUT:
					holderOut = (ViewHolderOut)convertView.getTag();
					break;
				}
			}
			
			FavoriteSmsListItem listItem = (FavoriteSmsListItem) convertView;
			switch(type){
			case GlobalConstant.TYPE_IN:
				listItem.bind(mContext, mDataList.get(position), holderIn.mFavSmsViewParent, holderIn.mBodyView, holderIn.mAddressView, holderIn.mDateView);
				break;
			case GlobalConstant.TYPE_OUT:
				listItem.bind(mContext, mDataList.get(position), holderOut.mFavSmsViewParent, holderOut.mBodyView, holderOut.mAddressView, holderOut.mDateView);
				break;
			}
			return convertView;
		}
	}

	private class ViewHolderIn {
		LinearLayout mFavSmsViewParent;
		TextView mBodyView;
		TextView mAddressView;
		TextView mDateView;
	}
	
	private class ViewHolderOut {
		LinearLayout mFavSmsViewParent;
		TextView mBodyView;
		TextView mAddressView;
		TextView mDateView;
	}


我的GlobalConstant.java中定义了type:

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


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


你可能感兴趣的:(android,ListView,BaseAdapter)