数据为空时点击 报错 看代码 case R.id.route_1: checkRoutePlanItemsValidity(mRoutePlanItems); mLayoutTab0.setSelected(false); mLayoutTab1.setSelected(true); mLayoutTab2.setSelected(false); interruptCountdown(); BaiduNaviManagerFactory.getRouteResultManager().selectRoute(1); BaiduNaviManagerFactory.getRouteResultManager().fullView(); mRouteList.clear(); mRouteList.addAll(mRouteDetails.get(1)); mResultAdapter.notifyItemChanged(1); break; 其中checkRoutePlanItemsValidity(mRoutePlanItems);具体代码如下: public void checkRoutePlanItemsValidity(List<BNRoutePlanItem> mRoutePlanItems) { if (mRoutePlanItems == null || mRoutePlanItems.size() < 1 || mRoutePlanItems.get(0) == null) { Log.d("TAGAA", "checkRoutePlanItemsValidity: 111"); return; } if (mRoutePlanItems.size() < 2 || mRoutePlanItems.get(1) == null) { Log.d("TAGAA", "checkRoutePlanItemsValidity: 222"); return; } if (mRoutePlanItems.size() < 3 || mRoutePlanItems.get(2) == null) { Log.d("TAGAA", "checkRoutePlanItemsValidity: 333"); return; } } 报错 java.lang.IndexOutOfBoundsException: Index: 1, Size: 0 帮我修改一下
时间: 2023-11-27 18:52:44 浏览: 102
根据报错信息 `java.lang.IndexOutOfBoundsException: Index: 1, Size: 0`,可以看出是因为 `mRoutePlanItems` 列表的大小为0,而在 `checkRoutePlanItemsValidity` 方法中需要访问列表的第一、二、三个元素,导致了越界异常。因此,你需要在调用 `checkRoutePlanItemsValidity` 方法之前,先对 `mRoutePlanItems` 列表进行非空判断。
修改后的代码如下:
```
case R.id.route_1:
if (mRoutePlanItems == null || mRoutePlanItems.size() < 1) {
// 对 mRoutePlanItems 进行非空判断
Log.d("TAGAA", "mRoutePlanItems is null or empty");
return;
}
checkRoutePlanItemsValidity(mRoutePlanItems);
mLayoutTab0.setSelected(false);
mLayoutTab1.setSelected(true);
mLayoutTab2.setSelected(false);
interruptCountdown();
BaiduNaviManagerFactory.getRouteResultManager().selectRoute(1);
BaiduNaviManagerFactory.getRouteResultManager().fullView();
mRouteList.clear();
mRouteList.addAll(mRouteDetails.get(1));
mResultAdapter.notifyItemChanged(1);
break;
```
阅读全文