-2

我收到此错误:

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();
}
4

1 回答 1

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);
}
于 2013-07-21T11:46:20.607 回答