Search code examples
javaandroidnullpointerexceptiononresume

Resume Activity - NullPointerException


I am having trouble on resuming an activity and my application crushes when i open it and i get a NullPointerException, which is strange because i have an !=null condition.

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_maps);//frag
    setContentView(R.layout.map_view);//linear
    // 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);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    gotoLocation(51.508182, -0.140315, DEFAULT_ZOOM);
    //mMap.setMyLocationEnabled(true);

}
    @Override
protected void onResume(){
    super.onResume();
    MapStateManager mgr = new MapStateManager(this);
    CameraPosition position = mgr.getSavedCameraPosition();
    if(position != null){
        CameraUpdate update = CameraUpdateFactory.newCameraPosition(position);
        mMap.moveCamera(update);
    }
}

The error is on line mMap.moveCamera(update);

MapStateManager.java

public class MapStateManager {

private static final String LONGITUDE = "longitude";
private static final String LATITUDE = "latitude";
private static final String ZOOM = "zoom";
private static final String BEARING = "bearing";
private static final String TILT = "tilt";
private static final String MAPTYPE = "MAPTYPE";

private static final String PREFS_NAME="mapCameraState";//definition for shared

private SharedPreferences mapStatePrefs;// save data on the device

public MapStateManager(Context context){
    mapStatePrefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
}

public void saveMapState(GoogleMap map){
    SharedPreferences.Editor editor = mapStatePrefs.edit();
    CameraPosition position = map.getCameraPosition();
    editor.putFloat(LATITUDE, (float) position.target.latitude);
    editor.putFloat(LONGITUDE, (float) position.target.longitude);
    editor.putFloat(TILT, position.tilt);
    editor.putFloat(ZOOM, position.zoom);
    editor.putFloat(BEARING, position.bearing);

    editor.commit();
}

public CameraPosition getSavedCameraPosition(){
    double latitude = mapStatePrefs.getFloat(LATITUDE, 0);
    double longitude = mapStatePrefs.getFloat(LONGITUDE, 0);

    if (latitude==0){
        return null;
    }
    LatLng target = new LatLng(latitude, longitude);

    float zoom = mapStatePrefs.getFloat(ZOOM, 0);
    float bearing = mapStatePrefs.getFloat(BEARING, 0);
    float tilt = mapStatePrefs.getFloat(TILT, 0);

    CameraPosition position = new CameraPosition(target, zoom, tilt, bearing);
    return position;
}

}


Solution

  • You check postion != null but mMap could still be null (which is what's happening).

    In your onResume() method you need to get the map again, as it may have been lost while your app was inactive.

    Try something like this:

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
    
        //Maybe move this to an else on the if below? I'm not sure exactly what this does
        gotoLocation(51.508182, -0.140315, DEFAULT_ZOOM);
    
        //Update to the saved position.
        MapStateManager mgr = new MapStateManager(this);
        CameraPosition position = mgr.getSavedCameraPosition();
        if(position != null){
            CameraUpdate update = CameraUpdateFactory.newCameraPosition(position);
            mMap.moveCamera(update);
        }
    }
    
    @Override
    protected void onResume(){
        super.onResume();
    
        //Get the map asynchronously again.
    
        // 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);
    }