大家好,我有一个异步任务,用于根据用户输入的地址指向地图上的一个点。我在 onPostExecute 中有一个 NullPointerException,我可以找出原因。这是异步类
private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{
@Override
protected List<Address> doInBackground(String... locationName) {
// Creating an instance of Geocoder class
Geocoder geocoder = new Geocoder(getBaseContext());
List<Address> addresses = null;
try {
// Getting only one address that matches the query
addresses = geocoder.getFromLocationName(locationName[0], 3);
} catch (IOException e) {
e.printStackTrace();
}
return addresses;
}
@Override
protected void onPostExecute(List<Address> addresses) {
if(addresses==null || addresses.size()==0){
Toast.makeText(getBaseContext(), "No Location found.Please check address", Toast.LENGTH_SHORT).show();
}
// Clears all the existing markers on the map
mMap.clear();
// Adding Markers on Google Map for each matching address
for(int i=0;i<addresses.size();i++){// this is line 585 from the logcat
Address address = (Address) addresses.get(i);
// Creating an instance of GeoPoint, to display in Google Map
LatLng latLng_search = new LatLng(address.getLatitude(), address.getLongitude());
String addressText = String.format("%s, %s",
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
address.getLocality());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng_search);
markerOptions.title(addressText);
mMap.addMarker(markerOptions);
// Locate the first location
if(i==0)
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng_search));
}
}
}
这是日志猫:
09-28 02:51:45.501: E/AndroidRuntime(16587): FATAL EXCEPTION: main
09-28 02:51:45.501: E/AndroidRuntime(16587): java.lang.NullPointerException
09-28 02:51:45.501: E/AndroidRuntime(16587): at drkstr.yar.Create_reminder_loc$GeocoderTask.onPostExecute(Create_reminder_loc.java:585)
09-28 02:51:45.501: E/AndroidRuntime(16587): at drkstr.yar.Create_reminder_loc$GeocoderTask.onPostExecute(Create_reminder_loc.java:1)
09-28 02:51:45.501: E/AndroidRuntime(16587): at android.os.AsyncTask.finish(AsyncTask.java:417)
09-28 02:51:45.501: E/AndroidRuntime(16587): at android.os.AsyncTask.access$300(AsyncTask.java:127)
09-28 02:51:45.501: E/AndroidRuntime(16587): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
09-28 02:51:45.501: E/AndroidRuntime(16587): at android.os.Handler.dispatchMessage(Handler.java:99)
09-28 02:51:45.501: E/AndroidRuntime(16587): at android.os.Looper.loop(Looper.java:143)
09-28 02:51:45.501: E/AndroidRuntime(16587): at android.app.ActivityThread.main(ActivityThread.java:4196)
09-28 02:51:45.501: E/AndroidRuntime(16587): at java.lang.reflect.Method.invokeNative(Native Method)
09-28 02:51:45.501: E/AndroidRuntime(16587): at java.lang.reflect.Method.invoke(Method.java:507)
09-28 02:51:45.501: E/AndroidRuntime(16587): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-28 02:51:45.501: E/AndroidRuntime(16587): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-28 02:51:45.501: E/AndroidRuntime(16587): at dalvik.system.NativeStart.main(Native Method)
错误出现在第 585 行,我已在代码中标记了这一点。该行中仅有的 2 个变量是 i 和 address,我检查 address 是否为空。我不明白为什么我仍然收到空指针异常。
如果您需要查看更多代码,请告诉我。
感谢您花时间阅读本文以及您可以提供的任何帮助。