android – How to use SupportMapFragment inside a Fragment?-ThrowExceptions

Exception or error:

I know that there has been an issue in using a nested fragment. But my application was designed to run on fragments and if i will be using activity for the map, my casting functions will have error.

I would like to ask help from you on how to achieve this. I’ve been searching in the internet but i couldn’t find best solution.

I’ve tried this code:

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) myFragmentActivity.getSupportFragmentManager().findFragmentById(
        // Check if we were successful in obtaining the map.
        if (mMap != null) {

this would give me duplicate error because of is a fragment inside my fragment.

So I look for a work around, this time, is a frame layout and in the run time I created the SupportMapFragment to it.

SupportMapFragment mSupportMapFragment = new SupportMapFragment();
            .replace(, mSupportMapFragment).commit();

though this does not give me a duplicate every time a close and open the fragment. but my error is that mSupportMapFragment.getMap is always null., I don’t get it why its null?.

mMap = mSupportMapFragment.newInstance().getMap();
        if (mMap != null){
            Log.e("ReportFragment","mMap is not empty");
            Log.e("ReportFragment","mMap is empty");

I would really appreciate any inputs from you guys, or do you have another work around but still in this process, i.e Fragment inside fragment



How to solve:

getMap() is deprecated

The code should be something like this in a Fragment:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_location, container, false);

    mMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(;

    return v;   


I just met my luck, while making this post,I found what Im looking for.

I have used this:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_location, container, false);
    mMapFragment = new SupportMapFragment() {
        public void onActivityCreated(Bundle savedInstanceState) {
            mMap = mMapFragment.getMap();
            if (mMap != null) {
    getChildFragmentManager().beginTransaction().add(, mMapFragment).commit();
return v;   

Credit to this Old post


I have used it in the given manner and it’s working fine.

It’s also working with getChildFragmentManager()


public class MapyFragment extends Fragment implements OnMapReadyCallback {

    private Context mContext;
    private SupportMapFragment supportMapFragment;
    private GoogleMap map;
    private MarkerOptions currentPositionMarker = null;
    private Marker currentLocationMarker;

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContext = getActivity();
        return inflater.inflate(R.layout.fragment_mapy, container, false);

    public void onActivityCreated(@Nullable Bundle savedInstanceState) {

        mContext = getActivity();

        FragmentManager fm = getActivity().getSupportFragmentManager();/// getChildFragmentManager();
        supportMapFragment = (SupportMapFragment) fm.findFragmentById(;
        if (supportMapFragment == null) {
            supportMapFragment = SupportMapFragment.newInstance();
            fm.beginTransaction().replace(, supportMapFragment).commit();

    public void onMapReady(GoogleMap googleMap) {
        map = googleMap;
        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

    public void updateCurrentLocationMarker(Location currentLatLng){

        if(map != null){

            LatLng latLng = new LatLng(currentLatLng.getLatitude(),currentLatLng.getLongitude());
            if(currentPositionMarker == null){
                currentPositionMarker = new MarkerOptions();

                        .title("My Location").
                currentLocationMarker = map.addMarker(currentPositionMarker);

            if(currentLocationMarker != null)


and fragment_mapy.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android=""
    map:uiZoomControls="true" />


In kotlin it could be done as follows:

class NameFragment: Fragment(){
    // other code ...
    private lateinit var googleMapsFragment: SupportMapFragment

    override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View {
         googleMapsFragment = childFragmentManager.findFragmentById( as SupportMapFragment
        // other code ...

Leave a Reply

Your email address will not be published. Required fields are marked *