mActivity.runOnUiThread(() -> { if (limitInfo != null) { for (int i = 0; i < limitInfo.size(); i++) { String[] arr = limitInfo.get(i).split(","); Log.e(TAG, "第" + arr[0] + "条路线限行消息:" + arr[1]); } } if (mRoutePlanItems != null) { if (mRoutePlanItems.size() > 0 && mRoutePlanItems.get(0) != null) { initTabView(mLayoutTab0, mRoutePlanItems.get(0)); } if (mRoutePlanItems.size() > 1 && mRoutePlanItems.get(1) != null) { initTabView(mLayoutTab1, mRoutePlanItems.get(1)); mLayoutTab1.setVisibility(View.VISIBLE); } else { mLayoutTab1.setVisibility(View.GONE); } if (mRoutePlanItems.size() > 2 && mRoutePlanItems.get(2) != null) { initTabView(mLayoutTab2, mRoutePlanItems.get(2)); mLayoutTab2.setVisibility(View.VISIBLE); } else { mLayoutTab2.setVisibility(View.GONE); } } mLayoutTab0.setSelected(true); mRouteList.clear(); mRouteList.addAll(mRouteDetails.get(0)); LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); mRecyclerView.setLayoutManager(layoutManager); mResultAdapter = new RouteResultAdapter(mRouteList); mRecyclerView.setAdapter(mResultAdapter); // 展示长途信息 if (BaiduNaviManagerFactory.getRouteResultManager().isLongDistance()) { mLDLayout.setVisibility(View.VISIBLE); } else { mLDLayout.setVisibility(View.GONE); } }); 报错 java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
时间: 2024-03-19 22:42:46 浏览: 120
这段代码中可能会出现 java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 异常,这是因为在访问 mRouteDetails 的时候,mRouteDetails 是一个空的列表,而你尝试访问其第一个元素。可以在使用 mRouteDetails 前先判断其是否为空,或者在使用前先对其进行初始化。
相关问题
new Thread(() -> { BNRoutePlanInfos routePlaneInfo = BaiduNaviManagerFactory.getRoutePlanManager().getRoutePlanInfo(); if (routePlaneInfo != null) { // 获取限行数据 limitInfo = routePlaneInfo.getLimitInfo(); //3Tab数据 mRoutePlanItems = routePlaneInfo.getRouteTabInfos(); //路线详情 mRouteDetails = routePlaneInfo.getRouteDetailLists(); } // 获取数据后更新 UI runOnUiThread(() -> { if (limitInfo != null) { for (int i = 0; i < limitInfo.size(); i++) { String[] arr = limitInfo.get(i).split(","); Log.e(TAG, "第" + arr[0] + "条路线限行消息:" + arr[1]); } } if (mRoutePlanItems != null) { if (mRoutePlanItems.size() > 0 && mRoutePlanItems.get(0) != null) { initTabView(mLayoutTab0, mRoutePlanItems.get(0)); } if (mRoutePlanItems.size() > 1 && mRoutePlanItems.get(1) != null) { initTabView(mLayoutTab1, mRoutePlanItems.get(1)); mLayoutTab1.setVisibility(View.VISIBLE); } else { mLayoutTab1.setVisibility(View.GONE); } if (mRoutePlanItems.size() > 2 && mRoutePlanItems.get(2) != null) { initTabView(mLayoutTab2, mRoutePlanItems.get(2)); mLayoutTab2.setVisibility(View.VISIBLE); } else { mLayoutTab2.setVisibility(View.GONE); } } mLayoutTab0.setSelected(true); mRouteList.clear(); mRouteList.addAll(mRouteDetails.get(0)); LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); mRecyclerView.setLayoutManager(layoutManager); mResultAdapter = new RouteResultAdapter(mRouteList); mRecyclerView.setAdapter(mResultAdapter); // 展示长途信息 if (BaiduNaviManagerFactory.getRouteResultManager().isLongDistance()) { mLDLayout.setVisibility(View.VISIBLE); } else { mLDLayout.setVisibility(View.GONE); } }); }).start(); 以上代码有问题吗? 如有不安全性问题,帮我修改一下
以上代码并没有明显的安全问题。但是建议将敏感信息进行加密传输或存储,比如限行数据和路线详情等。此外,建议将线程操作封装成方法,提高代码的可读性和可维护性。具体修改代码如下:
```
private void getRoutePlanInfo() {
new Thread(() -> {
BNRoutePlanInfos routePlaneInfo = BaiduNaviManagerFactory.getRoutePlanManager().getRoutePlanInfo();
if (routePlaneInfo != null) {
// 获取限行数据
limitInfo = routePlaneInfo.getLimitInfo();
//3Tab数据
mRoutePlanItems = routePlaneInfo.getRouteTabInfos();
//路线详情
mRouteDetails = routePlaneInfo.getRouteDetailLists();
}
// 获取数据后更新 UI
getActivity().runOnUiThread(() -> {
if (limitInfo != null) {
for (int i = 0; i < limitInfo.size(); i++) {
String[] arr = limitInfo.get(i).split(",");
Log.e(TAG, "第" + arr[0] + "条路线限行消息:" + arr[1]);
}
}
if (mRoutePlanItems != null) {
if (mRoutePlanItems.size() > 0 && mRoutePlanItems.get(0) != null) {
initTabView(mLayoutTab0, mRoutePlanItems.get(0));
}
if (mRoutePlanItems.size() > 1 && mRoutePlanItems.get(1) != null) {
initTabView(mLayoutTab1, mRoutePlanItems.get(1));
mLayoutTab1.setVisibility(View.VISIBLE);
} else {
mLayoutTab1.setVisibility(View.GONE);
}
if (mRoutePlanItems.size() > 2 && mRoutePlanItems.get(2) != null) {
initTabView(mLayoutTab2, mRoutePlanItems.get(2));
mLayoutTab2.setVisibility(View.VISIBLE);
} else {
mLayoutTab2.setVisibility(View.GONE);
}
}
mLayoutTab0.setSelected(true);
mRouteList.clear();
mRouteList.addAll(mRouteDetails.get(0));
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(layoutManager);
mResultAdapter = new RouteResultAdapter(mRouteList);
mRecyclerView.setAdapter(mResultAdapter);
// 展示长途信息
if (BaiduNaviManagerFactory.getRouteResultManager().isLongDistance()) {
mLDLayout.setVisibility(View.VISIBLE);
} else {
mLDLayout.setVisibility(View.GONE);
}
});
}).start();
}
```
以上方法是在 // 获取数据后更新 UI mActivity.runOnUiThread(() -> { } 中调用的
那么您可以将获取数据后更新UI的代码也放在onAttach()方法之后的runOnUiThread()中执行,这样就可以确保Fragment已经附加到Activity中了,避免出现Fragment未附加到Context的问题。具体代码如下:
```
private Context mContext;
@Override
public void onAttach(Context context) {
super.onAttach(context);
mContext = context;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_route_result, container, false);
LinearLayout tab1 = view.findViewById(R.id.tab1);
LinearLayout tab2 = view.findViewById(R.id.tab2);
initTabView(tab1, mBNRoutePlanList.get(0));
initTabView(tab2, mBNRoutePlanList.get(1));
return view;
}
private void initTabView(LinearLayout layoutTab, BNRoutePlanItem bnRoutePlanItem) {
TextView prefer = layoutTab.findViewById(R.id.prefer);
prefer.setText(bnRoutePlanItem.getPusLabelName());
TextView time = layoutTab.findViewById(R.id.time);
time.setText(getString(R.string.route_result_pass_time, (int) bnRoutePlanItem.getPassTime() / 60));
TextView distance = layoutTab.findViewById(R.id.distance);
distance.setText(getString(R.string.route_result_distance_text, (int) bnRoutePlanItem.getLength() / 1000));
TextView trafficLight = layoutTab.findViewById(R.id.traffic_light);
trafficLight.setText(String.valueOf(bnRoutePlanItem.getLights()));
}
private void updateUI() {
mActivity.runOnUiThread(() -> {
LinearLayout tab1 = getView().findViewById(R.id.tab1);
LinearLayout tab2 = getView().findViewById(R.id.tab2);
initTabView(tab1, mBNRoutePlanList.get(0));
initTabView(tab2, mBNRoutePlanList.get(1));
});
}
```
这样,即使在获取数据后更新UI时出现了Fragment未附加到Context的问题,也可以保证在Fragment附加到Activity后再更新UI了,避免了出现异常。
阅读全文