Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getSupportFragmentManager().findFragmentById(R.id.map) started returning null

I have a map fragment inside another fragment. This was working before, but I think it messed up after I updated google play services library. What is wrong? fragment_map.xml:

<fragment
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

MapFragment:

GoogleMap map;
private static View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    if (view != null) {
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null)
            parent.removeView(view);
    }
    try {

        view = inflater.inflate(R.layout.fragment_map, container, false);
        map = ((SupportMapFragment) getActivity()
                .getSupportFragmentManager()
                .findFragmentById(R.id.map))
                .getMap(); // NullPointerException at this line
        map.getUiSettings().setAllGesturesEnabled(true);
        map.getUiSettings().setMyLocationButtonEnabled(false);
        map.getUiSettings().setZoomControlsEnabled(true);
    } catch (InflateException e) {
        /* map is already there, just return view as it is */
    }

    return view;
}
like image 616
Gintas_ Avatar asked Feb 01 '15 13:02

Gintas_


1 Answers

I had to change

getActivity().getSupportFragmentManager()

to

getChildFragmentManager()

to get it working. No idea why it was working perfectly few days ago.

like image 111
Gintas_ Avatar answered Nov 15 '22 08:11

Gintas_