Android map in fragment nullpointer

后端 未结 2 1363
情书的邮戳
情书的邮戳 2021-01-26 08:11

In my project im trying to implementthis solution for managing a map in a dynamic added fragment.

public class MapFragment extends Fragment {
    private GoogleM         


        
相关标签:
2条回答
  • 2021-01-26 08:52

    Since you are using nested fragments, you should use getChildFragmentManager()

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

    And I recommend you to use getMapAsync method, because getMap is deprecated

    mapFragment.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap googleMap) {
            setupMap(googleMap);
        }
    });
    
    0 讨论(0)
  • 2021-01-26 09:01

    You need to use findViewById on the view you get, not using the fragment manager. The map is attached to the view

        View v =inflater.inflate(R.layout.fragment_map, container, false);
    
        map = v.findViewById(R.id.map))
                .getMap();
    
    0 讨论(0)
提交回复
热议问题