本文介绍了RuntimeException:执行doInBackground()时发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在接受用户的地址,然后尝试使用AsyncTask来获取经度和纬度.
I am accepting address by user and then trying to get latitude and longitude for that i am using 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
推荐答案
尝试检查addresses.size() > 0
if ((addresses != null) && (addresses.size() > 0)) {
Address address=addresses.get(0);
}
这篇关于RuntimeException:执行doInBackground()时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!