-3

I have an activity in my app which shows Google Map and there is some other functionality to select the location. The activity is named as MapActivity and it extends FragmentActivity.

public class MapActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap googleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_map);

    initializeMap();
    }

    private void initializeMap() {
        if (googleMap == null) {
            MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
            if(mapFragment!=null){
                mapFragment.getMapAsync(MapActivity.this);
            }

            // check if map is created successfully or not
            if (null== googleMap) {
                Toast.makeText(getApplicationContext(), "Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onMapReady(GoogleMap map) {
        googleMap = map;
        googleMap.setMyLocationEnabled(true);
    }
}

But when this activity is launched the app crashes as it mapFragment object in initializeMap method is returned null. This error has appeared since getMap() has been deprecated and getMapAsync() method is to be used but my mapFragment object it self is null.

How do inflate the map fragment to avoid this NullPointerException?

Aashish Gulabani
  • 141
  • 6
  • 24

1 Answers1

0
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}
AwaisMajeed
  • 2,254
  • 2
  • 10
  • 25
  • Thanks for the quick response. I went through the the link and changed the code from MapFragment to SupportMapFragment. But now my onMapReady method is not being called because of which my googleMap object is null. – Aashish Gulabani Apr 04 '17 at 10:55
  • Yes, i have declared it. It used to work fine earlier and map used to get load. But since the getMap method has been deprecated it had stopped working. – Aashish Gulabani Apr 07 '17 at 14:13