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内部究竟是什么 ...
随机推荐
- iOS9升级后第三方平台无法分享的问题
最近升级到了Xcode7,在真机调试中发现在初始化微博SDK时程序Crash. 解决办法从微博官网下一个最新的SDK,替换掉工程中的即可. 2.替换微博最新SDK之后成功运行程序,之后发现微信.QQ. ...
- Swift - 33 - 返回函数类型和函数嵌套
//: Playground - noun: a place where people can play import UIKit /*---------------------------返回函数类 ...
- Maven Profile标签
Maven Profiles标签可以针对不同的环境来使用不同的配置文件 在发布的时候可以用 mvn release -p product mvn release -p test mvn release ...
- php设计模式之简单工厂模式
①抽象基类:类中定义抽象一些方法,用以在子类中实现 ②继承自抽象基类的子类:实现基类中的抽象方法 ③工厂类:用以实例化所有相对应的子类 /** * * 定义个抽象的类,让子类去继承实现它 * */ a ...
- js 联系电话验证实现
var str=$('#tele').val(); var regPartton=/1[3-8]+\d{9}/; ...
- ExtJS5_自定义菜单2
这一节来定义另外三种类型的菜单类.首先定义菜单按钮类.文件放于app/view/main/region目录下面,文件名为ButtonMainMenu.js. /** * 显示在顶部的按钮菜单,可以切换 ...
- Keil增量编译build异常
装完MDK4.6/4.7a,发现没有修改源文件的情况下,单击build,每次都是重新编译,之前测试过重新建立工程/装回4.22,解决了该问题,当时并没多想原因.这次又出现了这个问题,重装了很久,也都这 ...
- Qt经典—线程、事件与Qobject(耳目一新)
介绍 You’re doing it wrong. — Bradley T. Hughes 线程是qt channel里最流行的讨论话题之一.许多人加入了讨论并询问如何解决他们在运行跨线程编程时所遇到 ...
- at命令
在windows系统中,windows提供了计划任务这一功能,在控制面板 -> 性能与维护 -> 任务计划, 它的功能就是安排自动运行的任务. 通过'添加任务计划'的一步步引导,则可建立一 ...
- R学习日记——分解时间序列(非季节性数据)
分解时间序列,就是将一个时间序列拆分成不同的构成元件.一般序列(非季节性序列)包含一个趋势部分和一个不规则部分(也就是随机部分),而如果是一个季节性序列,除以上两个外,还有季节性部分. 在此,我们 ...