getSupportFragmentManager().findFragmentById returns null for google maps in fragment in android?

11,965

The problem is that you're trying to use the Activity's FragmentManager, and you should be using the Fragment's child FragmentManager.

Remove the onCreate() override in the Fragment, and add an onCreateView() Override where you inflate the layout and call getMapAsync():

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

    View rootView = inflater.inflate(R.layout.fragment_map, container, false);

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

    mapFragment.getMapAsync(this);

    return rootView;
}
Share:
11,965
Vladimir88dev
Author by

Vladimir88dev

Updated on June 13, 2022

Comments

  • Vladimir88dev
    Vladimir88dev almost 2 years

    I have a problem with Google Maps, i.e. getSupportFragmentManager().findFragmentById returns always null. Do you have an idea how to solve this?

    Here is the code:

    fragment_map.xml:
    
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.myapp.something.MapFragment">
    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />                  
    </FrameLayout>
    

    MapsFragment.java:

    public class MapFragment extends Fragment implements OnMapReadyCallback, android.location.LocationListener
    
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        SupportMapFragment mapFragment = (SupportMapFragment) this.getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    ...
    

    I have Google Maps in Activity and it works with code:

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

    I am trying to reuse this in fragment, becouse I need maps in fragment, not in activity, but it doesn't work.

    I tried:

    • calling this code in "onCreateView" function
    • SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
    • GoogleMap mGoogleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap(); is deprecated, and application crashes
    • SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map); and similar variations, but in all cases I get null for mapFragment.

    Do you know how could I solve this problem?

  • Vladimir88dev
    Vladimir88dev almost 8 years
    Tnx for the reply, but this code also dosen't work. findFragmentById still returns null.
  • Vladimir88dev
    Vladimir88dev almost 8 years
    Done. Tnx again :)
  • Daniel Nugent
    Daniel Nugent almost 7 years
    @Aboulfotoh Your specific issue is probably different than the OP's issue here. How exactly does it not work for you?
  • Aboulfotoh
    Aboulfotoh almost 7 years
    @DanielNugent it give me a null reference object
  • Daniel Nugent
    Daniel Nugent almost 7 years
    @Aboulfotoh Does your layout xml look like fragment_map.xml in the question?
  • Aboulfotoh
    Aboulfotoh almost 7 years
    @DanielNugent Yes, exactly the same
  • Daniel Nugent
    Daniel Nugent almost 7 years
    @Aboulfotoh What exactly is null for you?
  • Aboulfotoh
    Aboulfotoh almost 7 years
    @DanielNugent it worked fine now, I was using ButterKnife and it cuases the problem
  • Fiphe
    Fiphe about 4 years
    I had the same problem. It took me a lot of time before seeing you answer. Thanks to you it worked. Thanks.
  • deadfish
    deadfish about 4 years
    But why? Why I should use getChildFragmentManager()?
  • Daniel Nugent
    Daniel Nugent about 4 years
    @deadfish It's just how they set up the framework. If you use a SupportMapFragment inside an activity, you can just use the activity fragment manager. If you use a SupportMapFragment inside of a fragment, you then have nested fragments, and you need to use the outer fragment's child fragment manager in order for it to work.