ADR
ADR

Reputation: 1291

NullPointerException with SupportMapFragment inside a Fragment

I am trying to get a Google Map inside of a Fragment, but I'm getting a NullPointerException. I have read other answers for similar questions but they haven't helped me.

HomeFragment:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager()
            .findFragmentById(R.id.ftMap);
        mapFragment.getMapAsync(this);

        return view;
    }

fragment_home.xml:

    <fragment
        android:id="@+id/ftMap"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_centerInParent="true"
        android:layout_margin="8dp" />

Upvotes: 2

Views: 78

Answers (1)

Daniel Nugent
Daniel Nugent

Reputation: 43322

Since you are using a SupportMapFragment nested inside of an outer Fragment, you need to use getChildFragmentManager() instead of getFragmentManager():

SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
     .findFragmentById(R.id.ftMap);

Upvotes: 3

Related Questions