我有一个这个架构的 Android应用程序:
我有一个Activity:MainActivity,这个包含一个FrameLayout.在这个FrameLayout中,我正在加载一个片段.这个名为Feed.在这个Feed片段中,我有一个谷歌地图片段.所以我想在我的feed.java上调用getMapAsync但是我无法得到我的地图分区.不知道我怎么能这样做?
MainActivity.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if(savedInstanceState == null){
Fragment fragment = null;
Class fragmentClass = null;
fragmentClass = Feeds.class;
this.setTitle("Fil d\'Actualité");
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
}
Feeds.java:
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
try {
if (googleMap == null) {
SupportMapFragment mF = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map));
mF.getMapAsync(this);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
在这段代码中,我的getChildFragmentManager().findFragmentById(R.id.map)总是返回null.
我还测试过在onCreate事件或onCreateView事件上编写此代码,但结果总是相同.
谢谢你的回答!
编辑feeds.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frameTest"
tools:context="com.findacomrade.comrade.viewController.fragments.Feeds">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fil d'actualité" />
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
activity.xml :
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.findacomrade.comrade.viewController.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<!--<include layout="@layout/content_main" />-->
<FrameLayout
android:id="@+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"/>
</android.support.design.widget.CoordinatorLayout>
最佳答案 您正在使用getSupportFragmentManager(),因此您的MapFragment也应来自支持库
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>