【问题标题】:java.lang.IndexOutOfBoundsException: using try, catchjava.lang.IndexOutOfBoundsException:使用try,catch
【发布时间】: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


【解决方案1】:

问题在于您的逻辑,即使您仍在调用的列表中有 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);}

    }

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      txt_locality_township.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();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-25
        • 2019-12-04
        • 1970-01-01
        • 2021-11-25
        • 1970-01-01
        • 2016-06-15
        • 2012-02-28
        • 2018-10-24
        相关资源
        最近更新 更多