GoogleMap not showing the changes I made

前端 未结 3 1388
日久生厌
日久生厌 2021-01-07 16:08

I have a fragment in which I use (or rather, want to use) Google Maps.

The fragment is between an actionbar and a tabhost, the fragment\'s layout is

3条回答
  •  甜味超标
    2021-01-07 16:40

    With some help from @AbhinavPuri I got it to work. What I had to do was

    Change

    
    

    to

    
    

    Made my fragment extend Fragment instead of extend SupportMapFragment and, because I use a GoogleMap in a fragment and not in an activity, I had to move the 'map getting' code to the onCreateView. If you don't, findFragmentById returns null, because in the onCreate, the view is not yet inflated.

    Now using this piece of code

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        /* If you place a mapfragment inside a fragment, it crashes when the fragment is
         * loaded a 2nd time. Below solution was found at http://stackoverflow.com/questions/
         * 14083950/duplicate-id-tag-null-or-parent-id-with-another-fragment-for-com-google-androi
         */
        if (view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null) {
                parent.removeView(view);
            }
        }
        try {
            // Inflate the layout for this fragment.
            view = inflater.inflate(R.layout.fragment_search_friends_map, container, false);
        } catch (InflateException e) {
            // Map is already there, just return view as it is.
        }
    
        SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    
        return view;
    }
    

    Using getChildFragmentManager because the SupportMapFragment is nested in another fragment.

提交回复
热议问题