AsyncTask类中的空指针异常

时间:2016-02-09 10:07:46

标签: android android-asynctask nullpointerexception

我在以下代码中得到NullPointerException。对于完整的程序,请参考http://wptrafficanalyzether.in/blog/showing-nearby-places-using-google-places-api-and-google-map-android-api-v2/

logcat的

FATAL EXCEPTION: main
                                                                            Process: com.example.android.googleplaces, PID: 2813
                                                                            java.lang.NullPointerException
                                                                                at com.example.android.googleplaces.MainActivity$ParserTask.onPostExecute(MainActivity.java:200)
                                                                                at com.example.android.googleplaces.MainActivity$ParserTask.onPostExecute(MainActivity.java:169)

和代码:

class ParserTask extends AsyncTask<String, Integer, List<HashMap<String,String>>>{

JSONObject jObject;

// Invoked by execute() method of this object
@Override
protected List<HashMap<String,String>> doInBackground(String... jsonData) {

List<HashMap<String, String>> places = null;
PlaceJSONParser placeJsonParser = new PlaceJSONParser();

try{
jObject = new JSONObject(jsonData[0]);

/** Getting the parsed data as a List construct */
places = placeJsonParser.parse(jObject);

}catch(Exception e){
Log.d("Exception",e.toString());
}
return places;
}

// Executed after the complete execution of doInBackground() method
@Override
protected void onPostExecute(List<HashMap<String,String>> list){

// Clears all the existing markers
if(mGoogleMap!=null)
mGoogleMap.clear();

for(int i=0;i<list.size();i++){

// Creating a marker
MarkerOptions markerOptions = new MarkerOptions();

// Getting a place from the places list
HashMap<String, String> hmPlace = list.get(i);

// Getting latitude of the place
double lat = Double.parseDouble(hmPlace.get("lat"));

// Getting longitude of the place
double lng = Double.parseDouble(hmPlace.get("lng"));

// Getting name
String name = hmPlace.get("place_name");

// Getting vicinity
String vicinity = hmPlace.get("vicinity");

LatLng latLng = new LatLng(lat, lng);

// Setting the position for the marker
markerOptions.position(latLng);

// Setting the title for the marker.
//This will be displayed on taping the marker
markerOptions.title(name + " : " + vicinity);

// Placing a marker on the touched position
if(mGoogleMap!=null)
mGoogleMap.addMarker(markerOptions);
}
}
}

2 个答案:

答案 0 :(得分:2)

好吧,如果它来自mGoogleMap.addMarker(markerOptions); line(并且您确定markeroptions变量不为null)这意味着您在地图未准备好时添加标记... 你应该在加载地图后调用asyncTask,如下面的代码所示:

    mGoogleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                @Override
                public void onMapLoaded() {
                    //start async task here 
                }
            });

希望这会有所帮助。

答案 1 :(得分:1)

更改此行

List<HashMap<String, String>> places = null;

List<HashMap<String, String>> places = new ArrayList<HashMap<String, String>>();

希望它能解决你的问题。