【发布时间】:2016-09-17 04:48:28
【问题描述】:
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at com.ppl.adapter.FoodRecyclerViewAdapter.onBindViewHolder(FoodRecyclerViewAdapter.java:99)
at com.ppl.adapter.FoodRecyclerViewAdapter.onBindViewHolder(FoodRecyclerViewAdapter.java:24)
什么错误?请解释我。食物有数据。 FoodCategory 也有数据。
在食物对象中;
private Integer id;
private String foodName;
// etc.
private List<FoodSize> foodSizes = new ArrayList<FoodSize>();
在 FoodSize 对象中;
private Integer price;
// etc
public Integer getPrice() {
return price;
}
在 FoodRecyclerViewAdapter 中;
List<Food> contents;
public void onBindViewHolder(ItemViewHolder holder, int position);
if (contents.get(position).getFoodSizes().get(0).getPrice() != null) {
holder.foodPrice.setText(contents.get(position).getFoodSizes().get(0).getPrice().toString());
}
这里;
[
{
"id": 1,
"food_name": "Chicken",
"food_category": { },
"food_sizes": [
{
"id": 84,
"size": "Normal",
"food_id": 1,
"price": 1100,
}
]
}
]
【问题讨论】:
-
可能你在
contents中什么都没有。你确定你已经成功解析了你的JSON吗? -
检查内容数组是否有值?
-
哪些是第 99 行和第 24 行,顺便问一下,你在哪里初始化
contents或foodSizes?请添加更多代码。 “这里”是什么意思,然后你发布 JSON?显示解析 JSON 的代码。 -
在内容中。这里是api。 :D 我使用改造