MainActivity开始。它首先调用vehicleRequest_validate()
然后调用Map Fragment。但是当调用vehicleRequest_validate()
时,createMarker启动时我也有一个空指针,这里mMapFragment.addMarker
..任何线索为什么?
MainActivity Class
VehicleRequest.vehicleRequest_validate();
mapFragment = new MapFragment(MainActivity.this, this);
manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.mainLayout, mapFragment).commit();
VehicleRequest Class
call.enqueue(new Callback<JsonElement>() {
@Override
public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
// success response
if(response.body().isJsonArray()){
JsonArray objectWhichYouNeed = response.body().getAsJsonArray();
System.out.println(objectWhichYouNeed);
for(int i = 0; i< response.body().getAsJsonArray().size(); i++){
JsonElement lat_array = response.body().getAsJsonArray().get(i);
JsonObject lat_obj = lat_array.getAsJsonObject();
Double lat = lat_obj.get("lat").getAsDouble();
JsonElement lng_array = response.body().getAsJsonArray().get(i);
JsonObject lng_obj = lng_array.getAsJsonObject();
Double lng = lng_obj.get("lng").getAsDouble();
createMarker(lat, lng);
}
public static void createMarker(Double latitude, Double longitude) {
BitmapDescriptor image = BitmapDescriptorFactory.fromResource(R.drawable.bus);
mMapFragment.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.anchor(0.5f, 0.5f)
.title("title")
.snippet("snippet")
.icon(image));
然后我的MapFragment