[英]Use google map support fragment in fragment
我目前正在做一个应用程序,其主要活动是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布局中的片段。
谢谢您的帮助,诚挚地,Matthieu Meunier
如果要在Fragment
使用SupportMapFragment
,则不能使用getFragmentManager
方法。 而是使用以下方法。
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.