【问题标题】:Use google map support fragment in fragment在片段中使用谷歌地图支持片段
【发布时间】:2016-10-20 17:08:56
【问题描述】:

我目前正在做一个应用程序,我的主要活动是 NavigationDrawer。因此,要浏览我的应用程序,任何其他“活动”都是一个片段,并且在其中一个片段中,我需要放置一个 GoogleMap 支持片段。关键是,在我的片段中,当我执行 getFragmentManager.findFragmentById(R.id.map); 时,它返回 null。

这是我的代码的样子。

//fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <fragment
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>
//My HomeFragment
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_home, container, false);

    FragmentManager manager = getFragmentManager();

    SupportMapFragment mapFragment = (SupportMapFragment)manager.findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    return view;
}

有人知道如何解决这个问题吗?我已经在这个网站和许多其他网站上寻找解决方案,但我不知道如何将片段存储在我的“父”片段的 xml 布局中。

感谢您的帮助, 亲切地, 马蒂厄·穆尼耶

【问题讨论】:

    标签: java android google-maps android-fragments


    【解决方案1】:

    如果要在 Fragment 中使用 SupportMapFragment,则不能使用 getFragmentManager 方法。请改用以下方法。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-06
      • 2020-12-21
      • 2017-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-26
      • 1970-01-01
      相关资源
      最近更新 更多