数据为空时点击 报错 看代码 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; } } 帮我修改一下
时间: 2023-11-27 11:52:41 浏览: 88
可以在方法内部加入一个判断,当传入的 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;
}
// 添加判断,当传入的 mRoutePlanItems 为空时直接返回
if (mRoutePlanItems.isEmpty()) {
Log.d("TAGAA", "checkRoutePlanItemsValidity: mRoutePlanItems is empty");
return;
}
}
```
阅读全文