我有一个FragmentActivity,我想在其中使用一个地图片段。我有一个问题,让支持片段管理器访问它。

 if (googleMap == null) {
            googleMap = ((SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map1)).getMap();

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }

            // create marker
            MarkerOptions marker = new MarkerOptions().position(
                    new LatLng(latitude, longitude)).title("Hello Maps ");

            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(new LatLng(latitude, longitude)).zoom(15).build();

            googleMap.animateCamera(CameraUpdateFactory
                    .newCameraPosition(cameraPosition));

            // adding marker
            googleMap.addMarker(marker);

当前回答

你可以在Fragments中使用childFragmentManager。

其他回答

getSupportFragmentManager()用于当您处于活动中并希望获得一个片段,但在您可以访问的片段中

getSupportFragmentManager()

通过使用另一个方法称为getFragmentMangaer()的工作方式与getSupportFragmentManager()一样,你可以像以前那样使用它:

fragmentTransaction =getFragmentManager().beginTransaction();
((AppCompatActivity) getActivity()).getSupportFragmentManager()

Kotlin用户尝试这个答案

(activity as AppCompatActivity).supportFragmentManager

简单的新方法做它在kotlin

requireActivity().supportFragmentManager

你所需要做的就是使用

getFragmentManager()

方法。它会给你支持片段管理器,当你在添加这个片段时使用它。

文档片段