4

Hello I'm trying to add MapView to LinearLayout this is my code:

GoogleMap mMap = mMapView.getMap();
if (mMap != null) {
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(-33.87365, 151.20689), 10);
    if (cameraUpdate != null)
        mMap.moveCamera(cameraUpdate);
    setUpMap(mMap);
}

documantation says; Wait for a GoogleMap to become available from a MapFragment or MapView that you have added to your application. You can verify that the GoogleMap is available by calling the getMap() method and checking that the returned object is not null.

But i still get

java.lang.NullPointerException: CameraUpdateFactory is not initialized.

is there any way to check cameraUpdateFactory is ready?

ankita patel
  • 4,201
  • 1
  • 13
  • 28
mavixce
  • 338
  • 3
  • 15

1 Answers1

11

You can force an initialization with the following code

    try {
        MapsInitializer.initialize(context);
    } catch (GooglePlayServicesNotAvailableException impossible) {
        /* Impossible */
    }

Place this snippet in your onCreate method to the top and it should work.

Greeny
  • 1,931
  • 2
  • 17
  • 26
  • I have this snippet in my Fragment's onAttach(), but I still see the question's exception in the wild (I can't reproduce it, myself). Unfortunately, I don't know whether the 'impossible' exception happened, or whether getMap() returned null. – android.weasel Nov 11 '13 at 13:49