Search code examples
androidgoogle-mapsgoogle-play-servicesandroid-5.0-lollipopandroid-maps-v2

findFragmentById(R.id.map) returns null in Android L


While testing a location based app in Android 4.4 we were able to properly see and use map via Android maps v2

Since the time I upgraded to Android L it is not able to fetch MapFragment with the following code:

  1. In MainActivity there's a FrameLayout in which I load ShowMapFragment.

  2. In ShowMapFragment there is a MapFragment to display map.

  3. Inside 'OnCreateView()' method mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map); map = mapFragment.getMap();

mapFragment is coming as null. Any help on this?


Solution

  • Got it. Posting this if anyone else faces the same issue. Basically we have to use getChildFragmentManager() instead of getFragmentManager() because the MapFragment is inside another fragemnt. See the answer by Rhisiart here.