我收到了这个错误:
07-21 11:05:21.484: E/AndroidRuntime(1329):
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
07-21 11:05:21.484: E/AndroidRuntime(1329):
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
我如何处理此类错误? 这是我的代码:
try {
List<Address> addresses = mGeocoder.getFromLocation(gettextLat, gettextLong, 1);
Log.e("TAG", addresses.get(0).getAddressLine(0) +
addresses.get(0).getAddressLine(1) +
addresses.get(0).getAddressLine(2));
String myAddress = addresses.get(0).getAddressLine(0) +
addresses.get(0).getAddressLine(1) + addresses.get(0).getAddressLine(2);
textAddresss.setText(myAddress);
} catch (IOException e) {
e.printStackTrace();
}
答案 0 :(得分:1)
您的ArrayList
为空。在尝试访问之前检查其size
List<Address> addresses = mGeocoder.getFromLocation(gettextLat, gettextLong, 1);
if (addresses.size() > 0) {
Log.e("TAG", addresses.get(0).getAddressLine(0)
+ addresses.get(0).getAddressLine(1)
+ addresses.get(0).getAddressLine(2));
String myAddress = addresses.get(0).getAddressLine(0)
+ addresses.get(0).getAddressLine(1)
+ addresses.get(0).getAddressLine(2);
textAddresss.setText(myAddress);
}