RuntimeException:执行 doInBackground() 时发生错误
RuntimeException: An error occured while executing doInBackground()
我正在接受用户的地址,然后尝试获取我正在使用 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.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.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.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
尝试检查addresses.size() > 0
if ((addresses != null) && (addresses.size() > 0)) {
Address address=addresses.get(0);
}
地址数组大小为 zero.it 表示您的地址数组不包含任何值,您正在尝试获取数组值的第一个位置。
在从数组中获取值之前需要检查 null 和大小,如下所示
if(addresses!=null && addresses.size()>0)
{
Address fetchedAddress = addresses.get(0);
}else
{
return yourcondition;
}
我正在接受用户的地址,然后尝试获取我正在使用 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.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.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.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
尝试检查addresses.size() > 0
if ((addresses != null) && (addresses.size() > 0)) {
Address address=addresses.get(0);
}
地址数组大小为 zero.it 表示您的地址数组不包含任何值,您正在尝试获取数组值的第一个位置。
在从数组中获取值之前需要检查 null 和大小,如下所示
if(addresses!=null && addresses.size()>0)
{
Address fetchedAddress = addresses.get(0);
}else
{
return yourcondition;
}