java.lang.IndexOutOfBoundsException:使用try,catch

java.lang.IndexOutOfBoundsException:使用try,catch,java,android,exception,indexoutofboundsexception,Java,Android,Exception,Indexoutofboundsexception,当我从服务器获取图像时,一些餐厅分别有1个菜单、4个菜单、5个菜单等。因此,我使用try catch进行IndexOutOfBoundsException,但如果餐厅有一个菜单,它不会在菜单下方显示另一个数据(例如位置)。第一个线性布局是菜单,第二个线性布局是位置,等等。如果餐厅有4个菜单,则显示位置。当我运行餐厅有1个菜单时,java.lang.IndexOutOfBoundsException:无效索引1,大小为1 在餐厅里 "id": "1", "menus_count":

当我从服务器获取图像时,一些餐厅分别有1个菜单、4个菜单、5个菜单等。因此,我使用
try catch
进行IndexOutOfBoundsException,但如果餐厅有一个菜单,它不会在菜单下方显示另一个数据(例如位置)。第一个线性布局是菜单,第二个线性布局是位置,等等。如果餐厅有4个菜单,则显示位置。当我运行餐厅有1个菜单时,
java.lang.IndexOutOfBoundsException:无效索引1,大小为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"
      }
    ],
在另一家餐厅

    "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)

问题在于您的逻辑,即使您仍在调用的列表中有一个菜单项

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

    }

我在这里写的代码非常糟糕,但我的目的是向您展示如何解决此问题,以及为什么在您的代码中出现此异常,此外,您还可以尝试在线性布局中动态生成ImageView,以改进此代码,这样即使有100个菜单页,您也可以增加图像的数量,而无需更改任何代码,并且布局中有静态菜单页。

我的建议是使用填充,而不是使用多个
线性布局
。因为你的菜单项是动态的。因此,您可以使用
RecyclerView
处理它。如果你发布屏幕截图,我可能会给出正确的解决方案。

只需将更新位置的行放在If块的顶部即可

txt_locality_town.setText(restaurant.getLocality()+“,”+ restaurant.getTownshipName())

未更新位置的原因是,如果引发IndexOutOfBoundsException,它将跳过此代码,因此位置不会更新

try {
    txt_locality_township.setText(restaurant.getLocality() + ", " + restaurant.getTownshipName());
    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);
    }

} catch (NullPointerException | IndexOutOfBoundsException e) {
    e.printStackTrace();
}

给我看看你的日志,为什么?我们知道问题出现在哪一行,以及原因。为什么不简单地检查大小是否正好为4,然后只获取这4个条目呢?我检查
restaurant.getMenus().get(0.getMenuImage()!=null&&restaurant.getMenus().get(0.getMenuImage().length()>0
。它不起作用。它起作用了,索引1表示菜单的大小必须为2,但您选中了>0,因为列表索引为零基数。若要获取列表中的第一项,则必须获取索引0