6
回答
Android ListView数组下标越界问题
做了个ListView查询加载数据的东西,我使用了分页,第一次加载时查询出数据放入到了mAdapter中mListView.addFooterView(loadMoreView);
mListView.setAdapter(mAdapter);
每次查询出10条数据,然后前台通过自定义的mAdapter.addItem()把新增的数据加入到mAdapter中,在调用mAdapter.notifyDataSetChanged();刷新下mAdapter,然后就是点击listView某项进入点击方法
mListView.setOnItemClickListener(new OnItemClickListener() {		
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
		Intent intent = new Intent(GoodsListActivity.this, GoodsDetailActivity.class);
		Bundle bund = new Bundle();				bund.putSerializable("ProductInfo",list.get(position));
		intent.putExtra("value",bund);
		startActivity(intent);
	}
});



举报

以下是问题补充:

  • @感觉太煽情 :然后第一页的时候没事,加载第二页,然后点击某项就会报空指针异常。 (2年前)
共有6个答案 最后回答: 2年前

目测你新加载的10个数据放到activity里的list里,再adpter.add(list)给adpter里增加10个

bund.putSerializable("ProductInfo",list.get(position));

这里就是10.get(22)错了,换成adapter.getItem(position);

--- 共有 1 条评论 ---
感觉太煽情是的,就是这个错,已改。 2年前 回复
异常信息呢
--- 共有 7 条评论 ---
感觉太煽情回复 @哼哼de那首歌 : 谢谢 2年前 回复
alexwan回复 @感觉太煽情 : http://pan.baidu.com/s/1kT7suJP 百度网盘 2年前 回复
alexwan回复 @感觉太煽情 : 我写了一个例子你可以看看,例子不知道怎么传给你。私聊我把 2年前 回复
感觉太煽情回复 @哼哼de那首歌 : 不能那么做,那样做的话,ListView如何加载更多显示。 2年前 回复
alexwan回复 @感觉太煽情 : 嗯 可能。试试 list.add();而不是使用adapter去add。然后 刷新madapter 2年前 回复

引用来自“哼哼de那首歌”的评论

异常信息呢
07-06 04:47:20.806: E/AndroidRuntime(1014): FATAL EXCEPTION: main
07-06 04:47:20.806: E/AndroidRuntime(1014): java.lang.IndexOutOfBoundsException: Invalid index 22, size is 10
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at java.util.ArrayList.get(ArrayList.java:304)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at com.shop.eshopservice.GoodsListActivity$2.onItemClick(GoodsListActivity.java:149)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at android.widget.AdapterView.performItemClick(AdapterView.java:292)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at android.widget.AbsListView$1.run(AbsListView.java:3168)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at android.os.Handler.handleCallback(Handler.java:605)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at android.os.Handler.dispatchMessage(Handler.java:92)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at android.os.Looper.loop(Looper.java:137)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at android.app.ActivityThread.main(ActivityThread.java:4340)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at java.lang.reflect.Method.invokeNative(Native Method)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at java.lang.reflect.Method.invoke(Method.java:511)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at dalvik.system.NativeStart.main(Native Method)



--- 共有 1 条评论 ---
alexwan你自定义的adapter addItem()方法中具体实现是怎么写的 2年前 回复

引用来自“哼哼de那首歌”的评论

异常信息呢

引用来自“感觉太煽情”的评论

07-06 04:47:20.806: E/AndroidRuntime(1014): FATAL EXCEPTION: main
07-06 04:47:20.806: E/AndroidRuntime(1014): java.lang.IndexOutOfBoundsException: Invalid index 22, size is 10
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at java.util.ArrayList.get(ArrayList.java:304)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at com.shop.eshopservice.GoodsListActivity$2.onItemClick(GoodsListActivity.java:149)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at android.widget.AdapterView.performItemClick(AdapterView.java:292)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at android.widget.AbsListView$1.run(AbsListView.java:3168)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at android.os.Handler.handleCallback(Handler.java:605)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at android.os.Handler.dispatchMessage(Handler.java:92)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at android.os.Looper.loop(Looper.java:137)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at android.app.ActivityThread.main(ActivityThread.java:4340)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at java.lang.reflect.Method.invokeNative(Native Method)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at java.lang.reflect.Method.invoke(Method.java:511)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-06 04:47:20.806: E/AndroidRuntime(1014): 	at dalvik.system.NativeStart.main(Native Method)



public class ProductAdapter extends BaseAdapter {
    ......
   public void addProductsItem(Product product){
     list.add(product);
  }
}



顶部