我目前正在开发一个应用程序,其主要 Activity 是 NavigationDrawer。因此,要浏览我的应用程序,任何其他“Activity ”都是一个 fragment ,并且在其中一个 fragment 中我需要放置一个 GoogleMap 支持 fragment 。关键是,当我在 fragment 中执行 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;
}
有人知道如何解决这个问题吗?我已经在这个网站和许多其他网站上寻找解决方案,但我不知道如何将 fragment 存储在我的“父” fragment 的 xml 布局中。
感谢您的帮助, 诚挚地, 马蒂厄·莫尼耶
最佳答案
如果您要在 Fragment
内使用 SupportMapFragment
,则不能使用 getFragmentManager
方法。请改用以下方法。
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
关于java - 在fragment中使用google map 支持fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40160478/