SupportMapFragment and onMapReady

Jordi Sipkens picture Jordi Sipkens · Jan 14, 2015 · Viewed 7.3k times · Source

Hee Guys,

I've been searching for a few hours now and tried different things, most of them I already had included. I hope you guys can help with something I'm apparently not seeing.

I have a SupportMapFragment inside my fragment for Maps. I used to have this working but for some reason it just crashes now with a nullpointer. After searching for hours and reading some more documentation I found out I use a deprecated way so I tried using the advised one : onMapReady. However it doesn't recognise the function even when I'm using

com.google.android.gms.maps.SupportMapFragment

Here is my xml with the fragment :

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frameLayoutMainAct"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <fragment
        android:id="@+id/mapView"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <View
        android:id="@+id/map_overlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

And here is the code starting in OnViewCreated :

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    if(GooglePlayServicesUtil.isGooglePlayServicesAvailable(MainActivity.getContext()) == ConnectionResult.SUCCESS) {
        initMap();
        if (locations != null) {
            addMarkers();
        }
        moveMapToCuracao();
    }
}

public void initMap() {
    final SupportMapFragment myMAPF = (SupportMapFragment) getFragmentManager()
            .findFragmentById(mapView);
    myMAPF.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap googleMap) {
            mGoogleMap = googleMap;

            mGoogleMap.setPadding(50, 0, 0, 0);
            mGoogleMap.getUiSettings().setMyLocationButtonEnabled(false);
            mGoogleMap.setMyLocationEnabled(true);
            mGoogleMap
                    .setOnMyLocationChangeListener(new OnMyLocationChangeListener() {

                        @Override
                        public void onMyLocationChange(Location location) {
                            // TODO Auto-generated method stub
                            GeomagneticField field = new GeomagneticField(
                                    (float) location.getLatitude(),
                                    (float) location.getLongitude(),
                                    (float) location.getAltitude(), System
                                    .currentTimeMillis());

                            // getDeclination returns degrees
                            mDeclination = field.getDeclination();
                        }

                    });

            if (locations != null) {
                addMarkers();
            }
            moveMapToCuracao();
        }
    });
}

UPDATE : Updated code thanks to Eugen

But stil crashes on getMapAsync() with a NullPointerException.

Answer

Eugen Pechanec picture Eugen Pechanec · Jan 14, 2015

The map fragment is part of a layout you inflate to another fragment (let's call it parent). The inflated fragment becomes a child fragment NOT of the activity BUT of the parent fragment. You have to ask the parent fragment's child fragment manager:

final SupportMapFragment myMAPF = (SupportMapFragment) getChildFragmentManager()
        .findFragmentById(mapView);