-1

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"/>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Gurvinder Singh
  • 2,157
  • 3
  • 15
  • 21
  • http://stackoverflow.com/questions/26597161/findfragmentbyid-for-supportmapfragment-returns-null-in-android-studio – vadher jitendra Jun 09 '16 at 07:30
  • 1
    Possible duplicate of [Android MapView getMap() returns null](http://stackoverflow.com/questions/22471766/android-mapview-getmap-returns-null) – AL. Jun 09 '16 at 07:31

3 Answers3

2

The getMap method is deprecated. You should use getMapAsync:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // ...

        ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
    }

     @Override
    public void onMapReady(final GoogleMap googleMap) {
        mGoogleMap = googleMap;

        // Do something with your map
    }
}
antonio
  • 18,044
  • 4
  • 45
  • 61
1

did you add the permissions and google API key ?

here is my code from project call this function in OnCreate()

private void CheckGoogleMap()
    {

        try {
            if (googleMap == null) {
                googleMap = ((MapFragment) getFragmentManager().
                        findFragmentById(R.id.map)).getMap();
            }
            googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

    }

in XML file

<fragment
                android:id="@+id/map"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                class="com.google.android.gms.maps.MapFragment"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true">

in Manifest file

 <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="com.example.googlemaps.permission.MAPS_RECEIVE" />
        <uses-permission android:name="android.permission.INTERNET"/>


    <permission         android:name="com.example.googlemaps.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-feature    android:glEsVersion="0x00020000" android:required="true" />

before closing of application TAG in Manifest file add this

 <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="YOUR API KEY" />

in Gradle add

compile 'com.google.android.gms:play-services:8.1.0'
Ahmad.Aslam90
  • 74
  • 1
  • 10
0

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();
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
Gurvinder Singh
  • 2,157
  • 3
  • 15
  • 21