我目前正在做一个应用程序,我的主要活动是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
发布于 2016-10-21 01:24:19
如果要在Fragment中使用SupportMapFragment,则不能使用getFragmentManager方法。请改用以下方法。
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);https://stackoverflow.com/questions/40160478
复制相似问题