I have an Android app with this architecture:
I have an Activity: MainActivity, this has a FrameLayout. In this FrameLayout, I am loading a fragment. This is called Feed. In this feed fragment, I have a Google map fragment. So I want to call getMapAsync on my feed.java, but I can’t get my map frame. Don't know how I can do this?
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();
}
}
In this code, my getChildFragmentManager (). findFragmentById (R.id.map) always returns null.
I also tested to write this code in the onCreate event or in the onCreateView event, but always with the same result.
Thank you for your responses!
edit 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">
<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>
<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>
source
share