protected void addSelectItemByPosition(int position){ addSelectItem(indexString[position],position); } 这个要紧吗 需要修改吗
时间: 2024-04-05 14:29:44 浏览: 7
在上面的代码中,我已经对 `addSelectItemByPosition()` 方法进行了修改,将 `position` 参数改为 `i + 1`,并在方法内部也进行了判断,避免了 `ArrayIndexOutOfBoundsException` 异常的发生。因此,你不需要对 `addSelectItemByPosition()` 方法进行额外的修改。
相关问题
protected void onActivityResult(int requestCode, int resultCode, Intent data)
这是Android中的一个方法,用于处理Activity的返回结果。其中,requestCode表示启动Activity时传递的请求码,resultCode表示返回结果的状态码,data则是返回的数据。你可以在这个方法中进行相应的处理,比如根据resultCode判断返回结果是否成功,然后根据requestCode取出对应的数据进行处理。
private void processPoiData() { if (mAllPoi.size() > 0) { for (int i = 0; i < mAllPoi.size(); i++) { addSelectItemByPosition(i); String name = mAllPoi.get(i).getName(); String[] items; if (name.contains("(")) { items = name.replace(")", "").split("\("); } else { items = name.split("-"); } for (String item : items) { addSelectItem(item, i); } } } } 在这调用了 报错 java.lang.ArrayIndexOutOfBoundsException: length=10; index=10 改一下
好的,你可以尝试将 `addSelectItemByPosition()` 方法中的 `position` 参数改为 `i + 1`,并在调用 `addSelectItemByPosition()` 方法之前先判断 `indexString` 数组的长度是否大于等于 `i + 1`,代码如下:
```
private void processPoiData() {
if (mAllPoi.size() > 0) {
for (int i = 0; i < mAllPoi.size(); i++) {
if (indexString.length >= i + 1) {
addSelectItemByPosition(i + 1);
}
String name = mAllPoi.get(i).getName();
String[] items;
if (name.contains("(")) {
items = name.replace(")", "").split("\\(");
} else {
items = name.split("-");
}
for (String item : items) {
addSelectItem(item, i);
}
}
}
}
protected void addSelectItemByPosition(int position) {
if (indexString.length >= position) {
addSelectItem(indexString[position - 1], position - 1);
}
}
```
这样可以避免 `ArrayIndexOutOfBoundsException` 异常的发生。