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
BaseAdapter的ArrayIndexOutOfBoundsException的更多相关文章
- Android BaseAdapter加载多个不同的Item布局时出现UncaughtException in Thread main java.lang.ArrayIndexOutOfBoundsException: length=15; index=15
java.lang.ArrayIndexOutOfBoundsException: length=15; index=15 异常出现的场景:在做聊天界面时,需要插入表情,图片,文字,名片,还有几种较为 ...
- Android笔记——BaseAdapter的使用
Android中的适配器(Adapter)是数据与视图(View)之间的桥梁,用于对要显示的数据进行处理,并通过绑定到组件进行数据的显示. BaseAdapter是Android应用程序中经常用到的基 ...
- Android关于listView的BaseAdapter以及getView的三级优化
1.4个重写方法的含义 自定义Adapter继承自BaseAdapter(通用适配器) getCount(); getItem(); getItemId(); getViewTypaCount() ...
- Android应用项目中BaseAdapter、SimpleAdapter和ArrayAdapter中的三种适配器
一.写在前面: 本次我们来讲解一下Android应用中三个适配器:BaseAdapter.SimpleAdapter和ArrayAdapter.其中常见的是BaseAdapter,也是个人推荐使用的适 ...
- ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0 cusAndComs=this.getEm().create ...
- Android日记-SimpleAdapter和BaseAdapter
SimpleAdapter 这是一个简单的适配器,可以将静态数据映射到XML文件中定义好的视图.你可以指定由Map组成的List(比如ArrayList)类型的数据.在ArrayList中的每个条目对 ...
- BaseAdapter的抽取
为了更方便高效的使用BaseAdapter,特意抽取了一下,下面是简单的结构图: 需要4个类: [MyBaseAdapter3]: public abstract class MyBaseAdapte ...
- Android开发学习——ListView+BaseAdapter的使用
ListView 就是用来显示一行一行的条目的MVC结构 * M:model模型层,要显示的数据 ----people集合 * V:view视图层,用户看到的界面 ...
- 深入理解使用ListView时ArrayAdapter、SimpleAdapter、BaseAdapter的原理
在使用ListView的时候,我们传给setAdapter方法的Adapter通常是ArrayAdapter.SimpleAdapter.BaseAdapter,但是这几个Adapter内部究竟是什么 ...
随机推荐
- OpenXmlSdk导出Excel
感觉OpenXmlSdk的语法真的不是很友好.研究了半天,只实现了简单的导出功能.对于单元格样式的设置暂时还是搞明白,网上的资料真的很少,官方文档是英文的.中文的文章大都是用工具(Open XML S ...
- 网站项目:让一般处理文件.ashx的代码有折叠功能(#region)
注意:该方法用于网站项目.但对于其他类型的项目有一定的参考作用. 1.首先在你想被别人访问的位置新建一个ashx文件,如/System/xxx.ashx. 新建xxx.ashx的代码如下: [csha ...
- 『重构--改善既有代码的设计』读书笔记----Introduce Explaning Variable
有时候你会遇到一系列复杂的表达式连续运算的时候,这个时候你可能根本招架不住如此长或者是如此复杂的长函数.这个时候你可以通过引用临时变量来储存他们的结果,将这些长函数的结果分成一个个临时变量来让函数清晰 ...
- Razor引擎的转换数据类型
AsInt() 把字符串转换为整数. if (myString.IsInt()) IsInt() {myInt=myString.AsInt();} AsFloat() 把字符串转换为浮点数. if ...
- CSS实现div居中
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- ubuntu 关闭开启防火墙
关闭防火墙: 命令: sudo ufw disable 打开防火墙 命令: sudo ufw enable
- Uva 120 - Stacks of Flapjacks(构造法)
UVA - 120 Stacks of Flapjacks Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld &a ...
- UVA - 524 Prime Ring Problem(dfs回溯法)
UVA - 524 Prime Ring Problem Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
- Android模拟器Genymotion安装向导
Genymotion简述 Genymotion提供Android虚拟环境的工具集.相信很多Android开发者一定受够了速度慢.体验差效率及其地下的官方模拟器了.如果你没有物理机器,又不想忍受官方模拟 ...
- 【问题】tableView的每组的头部不不能滚动的解决方案
group模式的解决方案 - (void)scrollViewDidScroll:(UIScrollView*)scrollView { if (scrollView == self.tableVie ...