简体   繁体   English

在片段中使用Google Map支持片段

[英]Use google map support fragment in fragment

I am currently doing an application with my main activity as a NavigationDrawer. 我目前正在做一个应用程序,其主要活动是NavigationDrawer。 So, to navigate through my application any other "activity" is a fragment and, in one of thoses fragment I need to put a GoogleMap support fragment. 因此,要浏览我的应用程序,其他任何“活动”都是一个片段,在其中一个片段中,我需要放置一个GoogleMap支持片段。 The point is that when, in my fragment, I do getFragmentManager.findFragmentById(R.id.map); 关键是,当我在片段中执行getFragmentManager.findFragmentById(R.id.map); it returns null. 它返回null。

Here is what my code looks like. 这是我的代码的样子。

//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;
}

Do anyone have an idea on how to fix this? 有谁知道如何解决这个问题? I already looked for solutions on this website and many others but I can't figure how to get the fragment stored in the xml layout of my "parent" fragment. 我已经在这个网站和许多其他网站上寻找解决方案,但是我不知道如何获取存储在“父”片段的xml布局中的片段。

Thanks for your help, Cordially, Matthieu Meunier 谢谢您的帮助,诚挚地,Matthieu Meunier

If you are to use a SupportMapFragment inside a Fragment you can't use getFragmentManager method. 如果要在Fragment使用SupportMapFragment ,则不能使用getFragmentManager方法。 Instead use the following method. 而是使用以下方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM