【问题标题】:ListView IndexOutOfBoundsExceptionListView IndexOutOfBoundsException
【发布时间】:2021-08-01 01:14:01
【问题描述】:

我在我的适配器内收到关于 IndexOutOfBoundsException 的崩溃报告,它从未在我的设备上发生过,所以我不知道这种情况。

在我的 getView 上它崩溃了(itemlist.get(position)):

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;

Object i = itemlist.get(position);

当然我会覆盖 getcount ,因为我提到它从未发生在我身上,但我有时会收到发生在用户身上的报告

public int getCount() {
    return itemlist.size();
}

任何想法怎么可能?以及如何预防?

【问题讨论】:

  • 请提供您的适配器代码!

标签: android android-listview crash indexoutofboundsexception


【解决方案1】:

您应该使用 parent 代替 itemlist 从列表中获取数据。

Object item = parent.getItemAtPosition(position);

String text=item.toString();

【讨论】:

  • parent 是我的 ViewGroup ,我并没有真正理解解决方案。
【解决方案2】:

我解决了。这是我的答案。 Indexoutofboundsexception with listview

你要做的是找出导致这个异常的视图。并确保一旦数据更改立即通知适配器!

【讨论】:

    猜你喜欢
    • 2014-02-08
    • 2016-03-17
    • 2015-06-23
    • 2016-05-01
    • 2014-12-08
    • 2015-05-14
    • 2016-01-01
    • 1970-01-01
    相关资源
    最近更新 更多