在我的Android应用程序,我有这样的代码:IndexOutOfBoundsException异常的名单<Address>
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();'是做什么的? –