将 JSON 数据提取到 mapview 中,我仍然在获取地图,但是没有地图位置,我在 JSON 文件中给出了,并且每当我运行我的应用程序时都会得到这个 - 不幸的是应用程序已经停止并且 Logcat 说: -
10-27 13:02:18.573: E/AndroidRuntime(719): Caused by: java.lang.NullPointerException
10-27 13:02:18.573: E/AndroidRuntime(719): at com.erachnida.restaurant.versionoct.MapView.onCreate(MapView.java:52)
请参阅以下代码:-
public class MapView extends MapActivity {
public GeoPoint point;
TapControlledMapView mapView; // use the custom TapControlledMapView
List<Overlay> mapOverlays;
Drawable drawable;
SimpleItemizedOverlay itemizedOverlay;
JSONObject json = null;
@Override
protected void onCreate(final Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mapOverlays = mapView.getOverlays();
drawable = getResources().getDrawable(R.drawable.ic_launcher);
itemizedOverlay = new SimpleItemizedOverlay(drawable, mapView);
itemizedOverlay.setShowClose(false);
itemizedOverlay.setShowDisclosure(true);
itemizedOverlay.setSnapToCenter(false);
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
HttpClient client = new DefaultHttpClient();
// Perform a GET request for a JSON list
HttpUriRequest request = new HttpGet(
"https://dl.***.com/maps.json");
// Get the response that sends back
HttpResponse response = null;
try {
response = client.execute(request);
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Convert this response into a readable string
String jsonString = null;
try {
jsonString = StreamUtils.convertToString(response
.getEntity().getContent());
} catch (IllegalStateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Create a JSON object that we can use from the String
JSONObject json = null;
try {
json = new JSONObject(jsonString);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
try {
JSONArray jsonArray = json.getJSONArray("maps");
Log.e("log_tag", "Opening JSON Array ");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String latitude = jsonObject.getString("latitude");
String longitude = jsonObject.getString("longitude");
String title = jsonObject.getString("title");
String country = jsonObject.getString("country");
double lat = Double.parseDouble(latitude);
double lng = Double.parseDouble(longitude);
Log.e("log_tag", "ADDING GEOPOINT" + title);
point = new GeoPoint((int) (lat * 1E6),
(int) (lng * 1E6));
OverlayItem overlayItem = new OverlayItem(point, title,
country);
itemizedOverlay.addOverlay(overlayItem);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
itemizedOverlay.populateNow();
mapOverlays.add(itemizedOverlay);
if (savedInstanceState == null) {
MapController controller = mapView.getController();
controller.setCenter(point);
controller.setZoom(7);
} else {
// example restoring focused state of overlays
int focused;
focused = savedInstanceState.getInt("focused_1", -1);
if (focused >= 0) {
itemizedOverlay.setFocus(itemizedOverlay
.getItem(focused));
}
}
}
}.execute();
}