首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在片段中使用google地图支持片段

在片段中使用google地图支持片段
EN

Stack Overflow用户
提问于 2016-10-21 01:08:56
回答 1查看 836关注 0票数 0

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

下面是我的代码。

代码语言:javascript
复制
//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布局中的片段。

非常感谢您的帮助,Matthieu Meunier

EN

回答 1

Stack Overflow用户

发布于 2016-10-21 01:24:19

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

代码语言:javascript
复制
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40160478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档