【发布时间】:2017-01-16 16:47:09
【问题描述】:
在我的 Android 应用中,我有以下代码:
LatLng[] branches;
String[] branchesArray = HomeActivity.branches.toArray(new String[HomeActivity.branches.size()]);
for (int i = 0; i < HomeActivity.branches.size(); i++) {
branches[i] = getLocationFromAddress(branchesArray[i]);
}
getLocationFromAddress 方法:
public LatLng getLocationFromAddress(String strAddress) {
Geocoder coder = new Geocoder(this);
List<Address> address;
LatLng p1 = null;
try {
address = coder.getFromLocationName(strAddress, 1);
if (address == null) {
return null;
}
Address location = address.get(0);
location.getLatitude();
location.getLongitude();
p1 = new LatLng((double) (location.getLatitude()), (double) (location.getLongitude()));
} catch (IOException e) {
Log.e("Error", e.getMessage());
}
return p1;
}
这段代码应该创建一个LatLng 的数组,从字符串地址数组中提取。问题是每当我运行这段代码时,我都会在日志中得到java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0。它将第 137 行称为有问题的行,即这一行:
Address location = address.get(0);
我该如何解决这个问题?
【问题讨论】:
-
为什么不直接使用
HomeActivity.branches.get(i)而不是branchesArray[i]? -
你觉得
location.getLatitude();和location.getLongitude();会怎么做?
标签: java android indexoutofboundsexception street-address