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

后端 未结 1 1276
没有蜡笔的小新
没有蜡笔的小新 2020-12-18 23:33

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:

相关标签:
1条回答
  • 2020-12-19 00:18

    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;
    }
    
    0 讨论(0)
提交回复
热议问题