moveCamera with CameraUpdateFactory.newLatLngBounds crashes-Collection of common programming errors

Ok I’m facing same issue.

I have my fragment with my SupportmapFragment, ABS, and navigation drawer.

What I did was:

public void resetCamera() {

    LatLngBounds.Builder builderOfBounds = new LatLngBounds.Builder();
    // Set boundaries ...
    LatLngBounds bounds = builderOfBounds.build();
    CameraUpdate cu;
    try{
        cu = CameraUpdateFactory.newLatLngBounds(bounds,10);
        map.animateCamera(cu); //This line will cause the exception first times when map is still not "inflated"
        System.out.println("Set with padding");
    } catch(IllegalStateException e) {
        e.printStackTrace();
        cu = CameraUpdateFactory.newLatLngBounds(bounds,400,400,0);
        map.animateCamera(cu);
        System.out.println("Set with wh");
    }

    //do the rest...
}

And, by the way, I’m calling resetCamera() from onCreateView after inflating and before returning.

What this does is catch the exception first time (while map “gets a size” as a way of saying it…) and then, other times I need to reset the camera, map already has size and does it through padding.

Issue is explained in documentation, it says:

Do not change the camera with this camera update until the map has undergone layout (in order for this method to correctly determine the appropriate bounding box and zoom level, the map must have a size). Otherwise an IllegalStateException will be thrown. It is NOT sufficient for the map to be available (i.e. getMap() returns a non-null object); the view containing the map must have also undergone layout such that its dimensions have been determined. If you cannot be sure that this has occured, use newLatLngBounds(LatLngBounds, int, int, int) instead and provide the dimensions of the map manually.

I think it’s a pretty decent solution. Hope it helps someone.