gpt4 book ai didi

android - ListView 的索引越界异常

转载 作者:太空狗 更新时间:2023-10-29 14:31:00 26 4
gpt4 key购买 nike

我刚遇到一个奇怪的 indexoutofboundsexception,我似乎无法重现。好像和我的listactivity有关:

java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
at java.util.ArrayList.get(ArrayList.java:311)
at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:229)
at android.widget.AbsListView.obtainView(AbsListView.java:1410)
at android.widget.ListView.makeAndAddView(ListView.java:1802)
at android.widget.ListView.fillDown(ListView.java:727)
at android.widget.ListView.fillSpecific(ListView.java:1359)
at android.widget.ListView.layoutChildren(ListView.java:1633)
at android.widget.AbsListView.onLayout(AbsListView.java:1263)
at android.view.View.layout(View.java:7088)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
at android.view.View.layout(View.java:7088)
at android.widget.FrameLayout.onLayout(FrameLayout.java:334)
at android.view.View.layout(View.java:7088)
at android.widget.FrameLayout.onLayout(FrameLayout.java:334)
at android.view.View.layout(View.java:7088)
at android.widget.FrameLayout.onLayout(FrameLayout.java:334)
at android.view.View.layout(View.java:7088)
at android.widget.FrameLayout.onLayout(FrameLayout.java:334)
at android.view.View.layout(View.java:7088)
at android.widget.FrameLayout.onLayout(FrameLayout.java:334)
at android.view.View.layout(View.java:7088)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
at android.view.View.layout(View.java:7088)
at android.widget.FrameLayout.onLayout(FrameLayout.java:334)
at android.view.View.layout(View.java:7088)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
at android.view.View.layout(View.java:7088)
at android.widget.FrameLayout.onLayout(FrameLayout.java:334)
at android.view.View.layout(View.java:7088)
at android.widget.FrameLayout.onLayout(FrameLayout.java:334)
at android.view.View.layout(View.java:7088)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1056)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1752)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)

我认为这可能与我添加/删除页脚 View 的方式有关。这是相关代码:

private void setLoadMoreFooter() {
resumeProgressBarAnimation(progressbar);
if (getListView().getFooterViewsCount() != 0 || currentCategory == null) {
try {
getListView().removeFooterView(footerView);
} catch (ClassCastException e) {
}
}
cursor = getVouchersFromDatabase();
Log.d(TAG, "Determining FOOTER status:\nCurrent page size: " + cursor.getCount()%10 + "\nCurrent category: " + currentCategory + "\nPage: " + currentCategory.getPage());
// Add footer if there are 10 vouchers on the current page
if (cursor.getCount()%10 == 0 && currentCategory != null) {
getListView().addFooterView(footerView, null, true);
}
else if (currentCategory.getPage() >= 2 && cursor.getCount()%10 != 0) {
getListView().addFooterView(footerView, null, false);
}
}

查看源码,好像是在headerviewlistadapter的getview方法这一行报错:

return mHeaderViewInfos.get(position).view;

有谁知道这可能是什么原因?

最佳答案

我通过确保在数据集更改时通知适配器来解决这个问题。在我的例子中,数据被另一个线程更改,在 ListView 检查数据是否正确之后。查看 ListView 源后。我在这里抛出了异常:

    public View More ...getView(int position, View convertView, ViewGroup parent) {
// Header (negative positions will throw an ArrayIndexOutOfBoundsException)
int numHeaders = getHeadersCount();
if (position < numHeaders) {
return mHeaderViewInfos.get(position).view;
}

// Adapter
final int adjPosition = position - numHeaders;
int adapterCount = 0;
if (mAdapter != null) {
adapterCount = mAdapter.getCount();
if (adjPosition < adapterCount) {
return mAdapter.getView(adjPosition, convertView, parent);
}
}

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

因此您可以看到 mFooterViewInfos.get(adjPosition - adapterCount).view 抛出 Indexoutofboundsexception。因为位置比 listview 想象的要大。

您要做的是找出导致此异常的 View 。并确保一旦数据更改立即通知适配器!

关于android - ListView 的索引越界异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6950069/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com