嘿大家, 我有一個異步任務,根據用戶輸入的地址指向地圖上的一個點。我在onPostExecute中有一個NullPointerException,我可以找出原因。這裏是異步類在我AsyncTask的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));
}
}
}
這裏是logcat的:
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個變量是我和地址,我檢查地址是否爲空。我不明白爲什麼我仍然得到空指針異常。
讓我知道如果你需要看到的代碼。
感謝您花時間閱讀本文,並感謝您提供的任何幫助。
您的地址爲空。這'地址= geocoder.getFromLocationName(locationName [0],3)'可能失敗。並使用活動上下文'Geocoder geocoder = new Geocoder(ActivtiyName.this)' – Raghunandan