Google Maps Fragment Returning Null Inside a Fragment

Google map called inside a fragment returns null

The solution.
I was placing a Map Fragment inside a Fragment.

So....

xml to be placed inside the fragment is

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

and ..

public class ContactFragment extends Fragment implements OnMapReadyCallback {

with in onCreateView()

MapFragment mapFragment = (MapFragment) getChildFragmentManager().findFragmentById(R.id.contactFrag_F_map);
mapFragment.getMapAsync(this);

And on OnMapReadyCallback

@Override
public void onMapReady(GoogleMap googleMap) {

theGoogleMap = googleMap;

setUpMap();
}

and below is the setUpMap.

public void setUpMap(){

theGoogleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
theGoogleMap.setMyLocationEnabled(true);
theGoogleMap.setTrafficEnabled(true);
theGoogleMap.setIndoorEnabled(true);
theGoogleMap.setBuildingsEnabled(true);
theGoogleMap.getUiSettings().setZoomControlsEnabled(true);
}

works fine.. :)

Google Maps Fragment returning null inside a fragment

This issue happened to me when I tried to port my Eclipse project to Android Studio project.

java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()' on a null object reference

I used it with a Fragment Activity like this:

((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.mapToday)).getMap();

However, the problem was, earlier I was using a specific version of Google Play Services (4.3.23).

But the Gradle build select the most recent version (compile 'com.google.android.gms:play-services:+')

I tried some other specific version like (6.1.71). But still gave null point exception.

I used the same old version as I used before and it worked.

compile 'com.google.android.gms:play-services:4.3.23'

This works for me as a temporary solution. However, this will be a problem when I need to update to a
new play services library.

It seems that there is no specific map library with this version available for Gradle build
when I try like this:

compile 'com.google.android.gms:play-services-maps:4.3.23'

Android Google Map in Fragment always show the SupportMapFragment was null

If you are using fragment inside another fragment then better to use getChildFragmentManager as it return a private FragmentManager for placing and managing Fragments inside of this Fragment.

supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.fragmentmap);

Reference: getChildFragmentManager

MapFragment return null

Update 1: getMap() is deprecated

It is better to use getMapAsync() method of MapFragment/SupportMapFragment. The example how to use the method shown below (copied from their documentation).

import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
import android.app.Activity;
import android.os.Bundle;

public class MapPane extends Activity implements OnMapReadyCallback {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity);

MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap map) {
LatLng sydney = new LatLng(-33.867, 151.206);

map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

map.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(sydney));
}

}

Quoting Google's MapFragment/SupportMapFragment

A GoogleMap can only be acquired using getMap() when the underlying
maps system is loaded and the underlying view in the fragment exists.
This class automatically initializes the maps system and the view;
however you cannot be guaranteed when it will be ready because this
depends on the availability of the Google Play services APK. If a
GoogleMap is not available, getMap() will return null.

On your code, you immediately retrieve GoogleMap AFTER Committing MapFragment. Wait until the MapFragment is fully loaded on activity so you can get the GoogleMap.

Perhaps, you can deliver the GoogleMap from MapFragment to Activity using interface, like this.

public class MyMapFragment extends SupportMapFragment
{
private MapCallback callback;

public void setMapCallback(MapCallback callback)
{
this.callback = callback;
}

public static interface MapCallback
{
public void onMapReady(GoogleMap map);
}

@Override public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
if(callback != null) callback.onMapReady(getMap());
}
}

public class MyActivity extends Activity implements MyMapFragment.MapCallback
{
// .........
@Override
public void onCreate(Bundle onsavedInstanceState)
{
mMapFragment = (MyMapFragment) getSupportFragmentManager()
.findFragmentByTag(MAP_FRAGMENT_TAG);

// We only create a fragment if it doesn't already exist.
if (mMapFragment == null) {
mMapFragment = MyMapFragment.newInstance();

mMapFragment.setMapCallback(this); // This activity will receive the Map object once the map fragment is fully loaded

// Then we add it using a FragmentTransaction.
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(MapLay.getId(), mMapFragment, MAP_FRAGMENT_TAG);
fragmentTransaction.commit();

}
else
{
mMapFragment.setMapCallback(this); // This activity will receive the Map object once the map fragment is fully loaded
}

@Override
public void onMapReady(GoogleMap map)
{
// Do what you want to map
}

}

Android : Support MapFragment Returns null

I've found the answer and it works for me

Instead of this

 SupportMapFragment fragment = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);

I've changed as

SupportMapFragment  fragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);

Google Maps getMap returns null

Since the 6.5 version of Play Services Google recommends using of asynchronous getting of GoogleMap and getMap() is now deprecated. As stkent's mentioned you have to call getMapAsync(this).

As this method asynchronous you need some callback for this method, that's why also you have to make your fragment implementing OnMapReadyCallback and in the method onMapReady() of these callbacks you can get reference to the map.

Also change line ((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(this); to ((SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map)).getMapAsync(this); 'cause inside fragment you have to use ChildFragmentManager.

MapFragment or MapView getMap() returns null on Lollipop

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);
}


Related Topics



Leave a reply



Submit