19

I've been using Google Maps API v2 for a long time on Android 4.x versions without a problem. Now I installed latest Lollipop build on my Nexus devices (5 and 7) trying to materialize the app.

I'd like to point out that everything is ok on KitKiat and the problem I'm describing is poping up only on Lollipop.

In my XML source code I'm using MapFragment (Google Play Services library version 6.1.11).

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

In Java code I'm overriding OnPause() method to reach map:

GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

At this line it throws NullPointerException. In debugger app is able to find fragment, however it's not able to return GoogleMap. I've also tried to use MapView. It also throws null. The weirdest thing for me is that map loads without a problem on app itself but in code I cant reach it to work with it.

mroczis
  • 1,879
  • 2
  • 16
  • 18

4 Answers4

60

I had exactly the same problem but this is what worked for me:

Replace this...

GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

with this...

GoogleMap map = getMapFragment().getMap();

then slip this bad boy in and give it a whirl...

private MapFragment getMapFragment() {
    FragmentManager fm = null;

    Log.d(TAG, "sdk: " + Build.VERSION.SDK_INT);
    Log.d(TAG, "release: " + Build.VERSION.RELEASE);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        Log.d(TAG, "using getFragmentManager");
        fm = getFragmentManager();
    } else {
        Log.d(TAG, "using getChildFragmentManager");
        fm = getChildFragmentManager();
    }

    return (MapFragment) fm.findFragmentById(R.id.map);
}
Rhisiart
  • 1,046
  • 12
  • 13
  • Thanks a lot. Finally this did the job for me. – Kartik Sharma Jan 25 '15 at 18:57
  • 1
    Is this a known issue on Lollipop? – Jay Sidri Feb 10 '15 at 08:30
  • @Rhisiart thanks for your answer .i want to know one more thing .after using this code ..how we will handle on backstack with this fragment in navigation drawer ...bcz after using this code onbackstack is not working with this fragment. – Deep Singh Jul 18 '15 at 11:01
  • @DeepSingh without more detail, all I can tell you is that my fragment extends com.google.android.gms.maps.MapFragment and is hosted within an android.support.v13.app.FragmentTabHost along with 2 other android.widget.TabHost$TabSpec's in an android.app.Activity targeting android:targetSdkVersion="22. You can see it in action within this app (select either 'Find nature events' or 'Find events' from the navigation drawer) - https://play.google.com/store/apps/details?id=org.wildlifetrusts.naturefinder – Rhisiart Jul 18 '15 at 14:40
  • 2
    I wish I could +1 multiple times on this answer – Shubham Chaudhary Jul 21 '15 at 07:44
  • I'm trying to implement this solution but it says getChildFragmentManager is undefined. Im inside an Activity. Someone hlp me please? – Frildoren Nov 25 '15 at 14:12
  • Dude, you saved my time. Thanks alot – Ramachandra Reddy Avula Jun 22 '16 at 13:05
13

Google now made a more convenient way to get the map using the following method

    myMapFragment.getMapAsync(new OnMapReadyCallback) {
         @Override
         public void onMapReady(GoogleMap googleMap) {
             myMap = googleMap;
         }
    });
Exception
  • 785
  • 8
  • 8
  • 3
    This was the correct answer in my case. The accepted answer seems to assume that the MapFragment is null, which is not the case according to the question (in the question the MapFragment is not null, but the returned value of getMap() is null). – BoD May 03 '15 at 21:52
3

It looks like this might be an issue with a targetSdkVersion of 21: https://code.google.com/p/android-developer-preview/issues/detail?id=1947

However, switching to getChildFragmentManager() worked for me:

findFragmentById for SupportMapFragment returns null in Android Studio

Community
  • 1
  • 1
bubastis
  • 271
  • 1
  • 5
1

Have you tried isGooglePlayServicesAvailable to check why its returning null? null has many reasons on getmap, try using this to check why its giving null

Luis Pereira
  • 1,481
  • 3
  • 19
  • 46
  • 1
    Yes, I did. Finally I solved my problem by reimplementing MapView. MapFragment does not seem to work at all. – mroczis Oct 27 '14 at 17:42
  • @mroczis Can u just describe me how did u fix it? – itsmewajid Oct 28 '14 at 07:30
  • 1
    I used MapView after all, set up it's lifecycle (calling mMapView.onCreate() on onCreate() method of activity, mMapView.onPause() on activity's onPause() etc.) and it did work. MapFragment does seem to not attach to FragmentManager on Lollipop. – mroczis Oct 28 '14 at 18:22