Sagon nicolas
Sagon nicolas

Reputation: 318

Get fragment in fragment layout android

I have an android application with this architecture :

I have an Activity : MainActivity, this one contain a FrameLayout. In this FrameLayout I'm loading a fragment. This one is named 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 framgment. Don't know how i can do it?

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 return null.

I have also tested to write this code on the onCreate event or the onCreateView event but always the same result.

Thanks for your answers!

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">

        <!-- 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>

Upvotes: 4

Views: 2029

Answers (2)

Kapil Rajput
Kapil Rajput

Reputation: 11545

Call your SupportMapFragment in onCreateView method of fragment class as :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
  v = inflater.inflate(R.layout.feed, container, false);

        if (googleMap == null) {
            SupportMapFragment mF = ((SupportMapFragment) v.getChildFragmentManager().findFragmentById(R.id.map));
            mF.getMapAsync(this);
        }
    }
   return v;
}

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157437

you are using getSupportFragmentManager(), so your MapFragment should be from the support library as well

<fragment
   android:id="@+id/map"     
   android:name="com.google.android.gms.maps.SupportMapFragment"
   android:layout_width="match_parent"
   android:layout_height="match_parent"/>

Upvotes: 1

Related Questions