java.lang.IndexOutOfBoundsException:使用try,catch

时间:2017-04-26 03:34:13

标签: java android exception indexoutofboundsexception

当我从服务器获取图像时,一些餐馆分别有1个菜单,4个菜单,5个菜单等。因此,我使用try catch作为IndexOutOfBoundsException,但如果餐厅有1个菜单,则它不会在菜单下方显示另一个数据(例如位置)。例如。第一个linearlayout是菜单,第二个linearlayout是位置等。如果餐厅有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"
      }
    ],

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

3 个答案:

答案 0 :(得分: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);}

    }

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

只需将更新位置的行放在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();
}