[英]java.lang.IndexOutOfBoundsException: using try, catch
當我從服務器獲取圖像時,一些餐館分別有1個菜單,4個菜單,5個菜單等。 因此,我使用try catch
for 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)
問題在於你的邏輯,即使你仍然在調用的列表中有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個菜單頁面而不更改任何代碼並且在布局中具有靜態菜單頁面。
我的建議是使用RecyclerView填充列表而不是使用多個LinearLayout
。 因為您的菜單項是動態的。 因此,您可以使用RecyclerView
處理它。 如果您發布截圖,那么我可能會給出正確的解決方案。
只需將更新位置的行放在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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.