我接受用户的地址,然后尝试获取我使用AsyncTask的经度和纬度。
每次我收到:RuntimeException: An error occured while executing doInBackground()
在此行遇到异常:Address fetchedAddress = addresses.get(0);
日志:
E/AndroidRuntime(810): FATAL EXCEPTION: AsyncTask #2
E/AndroidRuntime(810): Process: app.android.fields, PID: 810
E/AndroidRuntime(810): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime(810): at android.os.AsyncTask$3.done(AsyncTask.java:300)
E/AndroidRuntime(810): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
E/AndroidRuntime(810): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
E/AndroidRuntime(810): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
E/AndroidRuntime(810): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
E/AndroidRuntime(810): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
E/AndroidRuntime(810): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
E/AndroidRuntime(810): at java.lang.Thread.run(Thread.java:841)
E/AndroidRuntime(810): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
E/AndroidRuntime(810): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
E/AndroidRuntime(810): at java.util.ArrayList.get(ArrayList.java:308)
E/AndroidRuntime(810): at app.android.users.MainActivity$RegisterUser.doInBackground(MainActivity.java:326)
E/AndroidRuntime(810): at app.android.users.MainActivity$RegisterUser.doInBackground(MainActivity.java:1)
E/AndroidRuntime(810): at android.os.AsyncTask$2.call(AsyncTask.java:288)
E/AndroidRuntime(810): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
E/AndroidRuntime(810): ... 4 more
代码:
@Override
protected String doInBackground(String... args) {
............
strNewClientCompleteAddress =
strNewClientAddress+" "+strNewClientCity+" "+" "+strNewClientState+" "+strNewClientPostalCode;
Log.v("strNewClientCompleteAddress:",strNewClientCompleteAddress);
Geocoder geocoder= new Geocoder(MainActivity.this);
try {
List<Address> addresses = geocoder.getFromLocationName(strNewClientCompleteAddress, 1);
if(addresses != null) {
Address fetchedAddress = addresses.get(0);
lat = fetchedAddress.getLatitude();
lng = fetchedAddress.getLongitude();
Log.v("try-if", "ok great work");
}
else {
Log.v("try-else", "something wrong");
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.v("catch", "Could not get address....!");
}
strLat = String.valueOf(lat);
Log.v("lat:", strLat);
strLng = String.valueOf(lng);
Log.v("lng:", strLng);
........................
}
更新代码:
..............
if((addresses != null) && (addresses.size() > 0)) {
Address fetchedAddress = addresses.get(0);
lat = fetchedAddress.getLatitude();
lng = fetchedAddress.getLongitude();
Log.v("try-if", "ok great work");
}
..............
日志:
V/strNewClientCompleteAddress:(1119): 1701 Amphitheatre Pkwy Mountain View CA 94043
V/try-else(1119): something wrong
V/lat:(1119): 0.0
V/lng:(1119): 0.0
答案 0 :(得分:2)
尝试检查addresses.size() > 0
if ((addresses != null) && (addresses.size() > 0)) {
Address address=addresses.get(0);
}
答案 1 :(得分:1)
addresse数组大小为零。这意味着您的地址数组不包含任何值,并且您尝试获取数组值的第一个位置。
从数组中获取值之前需要检查null和size,如下所示
if(addresses!=null && addresses.size()>0)
{
Address fetchedAddress = addresses.get(0);
}else
{
return yourcondition;
}