android - 如何在 fragment 中访问 getSupportFragmentManager()?

标签 android android-fragments android-support-library android-fragmentactivity fragmentmanager

我有一个 FragmentActivity,我想在其中使用一个 map fragment 。我无法让支持 fragment 管理器访问它。

 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);

最佳答案

可以直接调用

getParentFragmentManager()  

获取 fragment 管理器。请注意,getFragmentManager() 也可以使用,但已被标记为已弃用。

关于android - 如何在 fragment 中访问 getSupportFragmentManager()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20237531/

相关文章:

Android 良好的 Activity/Fragment 通信实践 - GPS

android - 如何创建 Notification.Action 并将其添加到自定义通知

java - 安卓 BottomSheetBehavior setState() NullPointerException

android - 如何解决 Android 错误 "method setSupportActionBar in class AppCompatActivity cannot be applied to given types"?

java - 在 TextView 中设置结果时出现空指针异常

android - fragment 管理器 : No field with the name mListener is found in Animation class

Circle.ci 上带有 Google Play 服务的 Android AVD

Android FrameLayout 不会被 Fragments 取代

android - 手机找不到 Sensor.TYPE_LINEAR_ACCELERATION 的传感器

android - Android 中的 MVP - 在哪里放置 Google API 调用以获取位置服务?