在 AppCompatAcivity 中调用 mapfragment

Call a mapfragment in an AppCompatAcivity

我正在尝试在片段寻呼机的活动中使用 google 地图,因此地图位于寻呼机后面并且仍然能够与其交互,但无论我做什么,我都会得到 null例外。

我试过这样使用 SupportMapFragment:

SupportMapFragment mapFragment = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.Map));
        mapFragment.getMapAsync(this);

但是 getChildFragmentManager 无法解析,因为我猜是 AppCompatActivity。 还有另一种方法吗?可能吗?谢谢。

尝试动态添加它。

        MapFragment mMapFragment = MapFragment.newInstance();
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.llMapContainer, mMapFragment, "map");
        fragmentTransaction.commit();

否则你可以得到这样的地图片段

        mapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
        if (mapFragment != null) {
            mapFragment.getMapAsync(new OnMapReadyCallback() {
                @Override
                public void onMapReady(GoogleMap map) {
                   loadMap(map);
                }
           });
        }