在 Android 的 Fragment 布局中获取片段

4

我有一个 Android 应用程序,它的架构如下:

我有一个 Activity:MainActivity,其中包含一个 FrameLayout。在这个 FrameLayout 中,我正在加载一个名为 Feed 的 Fragment。在 Feed Fragment 中,我有一个 Google 地图 Fragment。因此,我想在 feed.java 中调用 getMapAsync,但我无法获取我的地图 Fragment。不知道该怎么做?

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>

1
请发布您的XML片段代码。 - karan
请同时发布您的Activity XML中相关的XML片段。 - gaborsch
2个回答

1
您正在使用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"/>

0
在片段类的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;
}

我无法在我的视图上调用getChildFragmentManager。public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_feeds, container, false); if (googleMap == null) { SupportMapFragment mF = ((SupportMapFragment) view.getChildFragmentManager().findFragmentById(R.id.map)); mF.getMapAsync(this); } return view; } - Sagon nicolas
只需将您的片段类扩展为SupportMapFragment,像这样:class Feed extends SupportMapFragment,而不是class Feed extends Fragment。 - Kapil Rajput

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接