IndexOutOfBoundsException

IndexOutOfBoundsException

当我从服务器获取图像时,一些餐厅分别具有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"
      }
    ],


餐厅Activity.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)

最佳答案

问题出在您的逻辑上,即使您仍在呼叫列表中的菜单项有1个

restaurant.getMenus().get(2)


这将使数组超出范围异常

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);
    }


应该

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);
        if(restaurant.getMenus().size()>=2){
        Picasso.with(context).load("http://ex}{ample.com/uploads/menus/" + restaurant.getMenus().get(1).getMenuImage()).into(img_menu_one);
        linearlayout_menu_one.setVisibility(View.VISIBLE);}
if(restaurant.getMenus().size()>=3){
        Picasso.with(context).load("http://example.com/uploads/menus/" + restaurant.getMenus().get(2).getMenuImage()).into(img_menu_two);
        linearlayout_menu_two.setVisibility(View.VISIBLE);}
if(restaurant.getMenus().size()>=4){
        Picasso.with(context).load("http://example.com/uploads/menus/" + restaurant.getMenus().get(3).getMenuImage()).into(img_menu_three);
        linearlayout_menu_three.setVisibility(View.VISIBLE);}

    }


我在这里编写的代码非常糟糕,但是我的目的是向您展示如何解决此问题以及代码中为什么会出现此异常,也可以尝试在Linear布局中动态制作ImageViews,以使此代码更好,这将提供即使有100个菜单页面而不更改任何代码,并且布局中具有静态菜单页面,您也可能会增加图像数量。

10-06 03:15