HeaderViewListAdapter随机崩溃

时间:2014-08-24 11:00:22

标签: android listview android-fragments

我在碰撞中遇到随机崩溃,我无法模拟它。它在Android V2和V4上随机崩溃,但非常非常

java.lang.IndexOutOfBoundsException: Invalid index 3, size is 0
   at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
   at java.util.ArrayList.get(ArrayList.java:311)
   at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:225)
   at android.widget.AbsListView.obtainView(AbsListView.java:1498)
   at android.widget.ListView.makeAndAddView(ListView.java:1749)
   at android.widget.ListView.fillSpecific(ListView.java:1294)
   at android.widget.ListView.layoutChildren(ListView.java:1580)
   at android.widget.AbsListView.onLayout(AbsListView.java:1349)
   at android.view.View.layout(View.java:7320)
   at android.widget.FrameLayout.onLayout(FrameLayout.java:342)
   at android.view.View.layout(View.java:7320)
   at android.widget.FrameLayout.onLayout(FrameLayout.java:342)
   at android.view.View.layout(View.java:7320)
   at android.widget.FrameLayout.onLayout(FrameLayout.java:342)
   at android.view.View.layout(View.java:7320)
   at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:801)
   at android.view.View.layout(View.java:7320)
   at android.widget.FrameLayout.onLayout(FrameLayout.java:342)
   at android.view.View.layout(View.java:7320)
   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1263)
   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1137)
   at android.widget.LinearLayout.onLayout(LinearLayout.java:1051)
   at android.view.View.layout(View.java:7320)
   at android.widget.FrameLayout.onLayout(FrameLayout.java:342)
   at android.view.View.layout(View.java:7320)
   at android.widget.FrameLayout.onLayout(FrameLayout.java:342)
   at android.view.View.layout(View.java:7320)
   at android.view.ViewRoot.performTraversals(ViewRoot.java:1162)
   at android.view.ViewRoot.handleMessage(ViewRoot.java:1882)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:130)
   at android.app.ActivityThread.main(ActivityThread.java:3701)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
   at dalvik.system.NativeStart.main(NativeStart.java)

索引3无效,大小为0 - 在crashlytics中索引是随机的。 我在片段中使用带有页眉和页脚的ListView。

onCreateView

mSearchBox = inflater.inflate(R.layout.search_box, mListView, false);
mListView.addHeaderView(mSearchBox, null, false);
mListView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();

我使用ListView for EditText中的标题在ListView中进行搜索(故意因为我希望EditText可以使用listview内容进行滚动)。我还使用addFooterView显示加载器(在列表中加载下一个项目时)。

您可以查看HeaderViewListAdapter第225行(GrepCode):

public View getView(int position, View convertView, ViewGroup parent) {
// ...
// ... code
// ...

// Footer (off-limits positions will throw an IndexOutOfBoundsException)
return mFooterViewInfos.get(adjPosition - adapterCount).view;
}

修改 我找到了类似的stackoverflow线程,但它对我没有帮助:Indexoutofboundsexception with listview 看起来像在ListView中使用带有片段的页眉或页脚时,android ListView有点麻烦。

1 个答案:

答案 0 :(得分:1)

在清除适配器之前,我必须在listview上设置null适配器:

mListView.setAdapter(null);
mAdapter.clear();
mAdapter.notifyDataSetChanged();
mListView.setAdapter(mAdapter);