【发布时间】:2017-04-26 03:34:12
【问题描述】:
当我从服务器获取图像时,一些餐厅分别有 1 个菜单、4 个菜单、5 个菜单等。因此,我将try catch 用于IndexOutOfBoundsException,但如果餐厅有1 个菜单,它不会在菜单下方显示其他数据(例如位置)。例如。第一个线性布局是菜单,第二个线性布局是位置,依此类推。如果餐厅有 4 个菜单,则会出现位置。当我经营这家餐厅时,有 1 个菜单,java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
在餐厅,
"id": "1",
"menus_count": 1,
"menus": [
{
"id": 27,
"menuImage": "e90573d2662eaa8d35b35c9cfcde2ee0.jpg"
}
],
在另一家餐厅,
"id": "2",
"menus_count": 5,
"menus": [
{
"id": 22,
"menuImage": "ee7e8f69f24dbba3c65d043192466be0.jpg"
},
{
"id": 20,
"menuImage": "64eaeb5c50f38c94ea65fe7e412047be.jpg"
},
{
"id": 12,
"menuImage": "5fc3b5f41a49da0281ee3f970cb64d26.jpg"
},
{
"id": 9,
"menuImage": "e56fa9fcbdeec32a94216e080de35952.jpg"
}
],
餐厅活动.java
try {
if (restaurant.getMenus() != null && restaurant.getMenus().size() > 0) {
Picasso.with(context).load("http://example.com/uploads/menus/" + restaurant.getMenus().get(0).getMenuImage()).into(img_menu_zero);
linearlayout_menu_zero.setVisibility(View.VISIBLE);
Picasso.with(context).load("http://example.com/uploads/menus/" + restaurant.getMenus().get(1).getMenuImage()).into(img_menu_one);
linearlayout_menu_one.setVisibility(View.VISIBLE);
Picasso.with(context).load("http://example.com/uploads/menus/" + restaurant.getMenus().get(2).getMenuImage()).into(img_menu_two);
linearlayout_menu_two.setVisibility(View.VISIBLE);
Picasso.with(context).load("http://example.com/uploads/menus/" + restaurant.getMenus().get(3).getMenuImage()).into(img_menu_three);
linearlayout_menu_three.setVisibility(View.VISIBLE);
}
txt_locality_township.setText(restaurant.getLocality() + ", " + restaurant.getTownshipName());
} catch (NullPointerException | IndexOutOfBoundsException e) {
e.printStackTrace();
}
详细,
04-26 10:08:38.896 26119-26119/com.eg.restaurant W/System.err: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
04-26 10:08:38.896 26119-26119/com.eg.restaurant W/System.err: at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
04-26 10:08:38.896 26119-26119/com.eg.restaurant W/System.err: at java.util.ArrayList.get(ArrayList.java:308)
04-26 10:08:38.896 26119-26119/com.eg.restaurant W/System.err: at com.wpg.hungryhopper.RestaurantActivity$1.success(RestaurantActivity.java:219)
04-26 10:08:38.896 26119-26119/com.eg.restaurant W/System.err: at com.wpg.hungryhopper.RestaurantActivity$1.success(RestaurantActivity.java:150)
04-26 10:08:38.896 26119-26119/com.eg.restaurant W/System.err: at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45)
04-26 10:08:38.896 26119-26119/com.eg.restaurant W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
04-26 10:08:38.896 26119-26119/com.eg.restaurant W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-26 10:08:38.896 26119-26119/com.eg.restaurant W/System.err: at android.os.Looper.loop(Looper.java:135)
04-26 10:08:38.896 26119-26119/com.eg.restaurant W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5669)
04-26 10:08:38.896 26119-26119/com.eg.restaurant W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-26 10:08:38.896 26119-26119/com.eg.restaurant W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
04-26 10:08:38.896 26119-26119/com.eg.restaurant W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
04-26 10:08:38.896 26119-26119/com.eg.restaurant W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
【问题讨论】:
-
显示你的 logcat。
-
为什么?我们知道问题出在哪条线路上,以及问题的原因。
-
那么你为什么不简单地检查一下 size 是否正好是 4 来只获取这 4 个条目呢?
-
我查看
restaurant.getMenus().get(0).getMenuImage() != null && restaurant.getMenus().get(0).getMenuImage().length() >0。它不起作用。 -
它的工作,索引 1 是意味着你的菜单的大小必须是 2 但你检查了 > 0,因为列表索引是零基如果你想获得列表中的第一个项目,你必须获得 0 的索引跨度>
标签: java android exception indexoutofboundsexception