Gurvinder Singh Gurvinder Singh - 4 months ago 25
Android Question

getMap() is null in SupportMapFragment in gogole map in andorid fragment

I am using google map in a fragment and getting null when calling getMap(), this is my code:

In my MapViewFragment

private GoogleMap mGoogleMap;


I am getting null at this line, in onCreate() method...

mGoogleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();


In fragment_map_view file

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


Please suggest.

Answer

I got the reason for it, I think I should post for future readers......

Actually as I GoogleMap was called in a fragment so, in the line..

mGoogleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

instead of

getFragmentManager(), getSupportFragmentManager() should be used in fragments.

Corrected one-

mGoogleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();