Android ListView addScrapView ArrayIndexOutOfBoundsException
2014-03-18 18:13
621 查看
今天在使用ListView 自定义Adapter的时候出了一个ArrayIndexOutOfBoundsException错误,错误信息如下:
java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:5996)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4554)
at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:3874)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
这里先贴出有问题的代码:
private class HistoryAdapter extends BaseAdapter {
public LayoutInflater inflater;
static final int CLEAR_ITEM = 1;
static final int COMMON_ITEM = 2;
public HistoryAdapter(Context ctx) {
this.inflater = LayoutInflater.from(ctx);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return historyKey.length + 1;
}
@Override public int getViewTypeCount() { // TODO Auto-generated method stub return 2; } @Override public int getItemViewType(int position) { if(position!= historyKey.length){ return COMMON_ITEM; }else{ return CLEAR_ITEM; } }
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolderH holder;
if (convertView == null) {
holder = new ViewHolderH();
if(getItemViewType(position)==COMMON_ITEM){
convertView = inflater.inflate(R.layout.list_search_history_item, null);
holder.tv_key = (TextView) convertView.findViewById(R.id.tv_key);
}else{
convertView = inflater.inflate(R.layout.list_search_history_footer, null);
}
convertView.setTag(holder);
} else {
holder= (ViewHolderH) convertView.getTag();
}
LogUtil.i(TAG, "position="+position);
if(getItemViewType(position)==COMMON_ITEM){
holder.tv_key.setText(historyKey[position]);
}else{
LogUtil.i(TAG, "position="+position +" last");
}
return convertView;
}
}
static class ViewHolderH {
TextView tv_key;
}
反复检查了半天代码,没觉得哪里写的问题,最后在stackoverflow上找到了答案。
大概解释如下:
HistoryAdapter 的
getViewTypeCount() 方法返回值为2,表示ListView中有两种不同的视图,
getItemViewType() 的返回值分别为1或2, 取决于item在ListView中的位置。Adapter 的 getItemViewType() 的返回值应该是从下表0开始的, 所以应该返回0或者1。
解决办法:
static final int CLEAR_ITEM = 0;
static final int COMMON_ITEM = 1;
将getItemViewType() 的返回值改为 0或者1,运行不报错了,问题解决啦!
最后总结一点是:Adapter的getViewTypeCount 方法和getItemViewType方法返回值之间存在一定的关系。如果getViewTypeCount 返回值为 3,那么getItemViewType方法的返回值应该是 0、1、2,不能超过2,否则会出现ArrayIndexOutOfBoundsException异常。
java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:5996)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4554)
at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:3874)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
这里先贴出有问题的代码:
private class HistoryAdapter extends BaseAdapter {
public LayoutInflater inflater;
static final int CLEAR_ITEM = 1;
static final int COMMON_ITEM = 2;
public HistoryAdapter(Context ctx) {
this.inflater = LayoutInflater.from(ctx);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return historyKey.length + 1;
}
@Override public int getViewTypeCount() { // TODO Auto-generated method stub return 2; } @Override public int getItemViewType(int position) { if(position!= historyKey.length){ return COMMON_ITEM; }else{ return CLEAR_ITEM; } }
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolderH holder;
if (convertView == null) {
holder = new ViewHolderH();
if(getItemViewType(position)==COMMON_ITEM){
convertView = inflater.inflate(R.layout.list_search_history_item, null);
holder.tv_key = (TextView) convertView.findViewById(R.id.tv_key);
}else{
convertView = inflater.inflate(R.layout.list_search_history_footer, null);
}
convertView.setTag(holder);
} else {
holder= (ViewHolderH) convertView.getTag();
}
LogUtil.i(TAG, "position="+position);
if(getItemViewType(position)==COMMON_ITEM){
holder.tv_key.setText(historyKey[position]);
}else{
LogUtil.i(TAG, "position="+position +" last");
}
return convertView;
}
}
static class ViewHolderH {
TextView tv_key;
}
反复检查了半天代码,没觉得哪里写的问题,最后在stackoverflow上找到了答案。
大概解释如下:
HistoryAdapter 的
getViewTypeCount() 方法返回值为2,表示ListView中有两种不同的视图,
getItemViewType() 的返回值分别为1或2, 取决于item在ListView中的位置。Adapter 的 getItemViewType() 的返回值应该是从下表0开始的, 所以应该返回0或者1。
解决办法:
static final int CLEAR_ITEM = 0;
static final int COMMON_ITEM = 1;
@Override public int getViewTypeCount() { // TODO Auto-generated method stub return 2; } @Override public int getItemViewType(int position) { if(position!= historyKey.length){ return COMMON_ITEM; }else{ return CLEAR_ITEM; } }
将getItemViewType() 的返回值改为 0或者1,运行不报错了,问题解决啦!
最后总结一点是:Adapter的getViewTypeCount 方法和getItemViewType方法返回值之间存在一定的关系。如果getViewTypeCount 返回值为 3,那么getItemViewType方法的返回值应该是 0、1、2,不能超过2,否则会出现ArrayIndexOutOfBoundsException异常。
相关文章推荐
- ArrayIndexOutOfBoundsException (@AbsListView$RecycleBin:addScrapView:7374) {main}
- ListView中使用type需要注意的东西 java.lang.ArrayIndexOutOfBoundsException: length=2; index=2 addScrapView
- ListView中使用type需要注意的东西 java.lang.ArrayIndexOutOfBoundsException: length=2; index=2 addScrapView
- 【Android】ListView ViewHolder ArrayIndexOutOfBoundsException: length=2; index=2
- ArrayIndexOutOfBoundsException with custom Android Adapter for multiple views in ListView
- Android listview ArrayIndexOutOfBoundsException问题
- TextView 显示内容时出现 ArrayIndexOutOfBoundsException 的解决方法(Android 4.1)
- Android 4.1/4.1.1 TextView.setText for Html 引发ArrayIndexOutOfBoundsException的问题
- android listview Caused by: java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
- Android ListView多布局复用ArrayIndexOutOfBoundsException问题
- Android问题集-RecyclerView--java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1
- 安卓listview两个类型条目的复用,总是报数组越界Android listview ArrayIndexOutOfBoundsException问题
- [置顶] android使用getItemViewType时出现java.lang.ArrayIndexOutOfBoundsException的错误
- TextView 显示内容时出现 ArrayIndexOutOfBoundsException 的解决方法(Android 4.1)
- ListView getItemViewType ArrayIndexOutOfBoundsException 的解决办法
- Android 解决Listview ArrayIndexOutOfBoundsException
- ArrayIndexOutofBound AbsListView$RecycleBin.addScrapView
- java.lang.ArrayIndexOutOfBoundsException: length=13; index=14 android.widget.AbsListView$RecycleBin.
- ListView用getItemViewType()区分不同item布局的时候出现ArrayIndexOutOfBoundsException
- android BaseAdapter java.lang.ArrayIndexOutOfBoundsException!!!!!!!!!