Android SortedList java.lang.IndexOutOfBoundsException:要求获取 0 处的项目,但大小为 0

问题描述 投票:0回答:1
public Order gimmeThePendingOrderToDeliver(String _id, String my_phone) {
        for (int i = 0; i < orderSortedList.size(); i++) {
            Order o = orderSortedList.get(i);

            boolean a = o.isCanceled_by_user(), b = o.isCanceled_by_owner(), c = o.isDelivered();
            String d = o.getDelivery_guy();
            if (_id.equals(o.get_id()) && !a && !b && !c && d.equals(my_phone)) {
                Toast.makeText(context, "IM HERE", Toast.LENGTH_SHORT).show();
                return o;
            }
        }
        Toast.makeText(context, orderSortedList.get(0).get_id(), Toast.LENGTH_SHORT).show();
        return null;
    }

无法迭代 Android SortedList 返回标题中所述的错误,任何帮助将不胜感激。预先感谢。

android sortedlist
1个回答
0
投票

orderSortedList
为空时,它不会进入
for
循环,因此,它会立即到达最底部的
Toast
语句,其中
orderSortedList.get(0)
会生成
IndexOutOfBoundsException
,因为列表为空。

© www.soinside.com 2019 - 2024. All rights reserved.