Java 在片段布局中获取片段

Java 在片段布局中获取片段,java,android,android-fragments,Java,Android,Android Fragments,我有一个android应用程序,其架构如下: 我有一个活动:MainActivity,这一个包含框架布局。在这个框架布局中,我加载了一个片段。这一个叫做Feed。在这个提要片段中,我有一个谷歌地图片段。因此,我想在feed.java上调用getMapAsync,但无法获取我的映射框架。不知道我该怎么做 MainActivity.java: protected void onCreate(Bundle savedInstanceState) { super.onCreate(sav

我有一个android应用程序,其架构如下:

我有一个活动:MainActivity,这一个包含框架布局。在这个框架布局中,我加载了一个片段。这一个叫做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>

activity.xml:

您使用的是
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"/>

在fragment类的onCreateView方法中调用SupportMapFragment,如下所示:

@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;
}

发布片段的xml代码请同时发布活动xml中的相关xml片段。我无法在视图中调用getChildFragmentManager<代码>CreateView上的公共视图(LayoutFlater充气器,ViewGroup容器,Bundle savedInstanceState){//充气此碎片视图的布局视图=充气器。充气(R.layout.fragment_feeds,container,false);如果(googleMap==null){SupportMapFragment mF=((SupportMapFragment)view.getChildFragmentManager().findFragmentById(R.id.map));mF.getMapAsync(this);}返回视图;}只需使用SupportMapFragment扩展片段类,就像这样类提要扩展SupportMapFragment而不是类提要扩展片段