抽屉片段的容器是空的,所以我不能膨胀它

Drawer fragment's container is null so I can't inflate it

我有以下内容:

activity_main.xml: 包含 framelayout 和一个 fragment 来保存抽屉,它不仅仅是一个 listview

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout android:id="@+id/container" android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <fragment android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
        android:layout_gravity="start"
        android:name="com.example.example.drawer.DrawerFragment"
        tools:layout="@layout/drawer_fragment" />

</android.support.v4.widget.DrawerLayout>

drawer_fragment.xml 列表视图顶部有图像

<LinearLayout 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:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/profilePhoto"
        android:layout_width="match_parent"
        android:contentDescription="@null"
        android:layout_height="128dp" />

    <ListView
        android:id="@+id/lvOptions"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="3dp"
        android:background="#cccc"
        tools:context=".NavigationDrawerFragment" />

</LinearLayout>

MainActivity的setContentView(R.layout.activity_main);执行时,也会自动执行drawer fragment的onCreateView,但是container为null。我执行以下操作:

View rootView = inflater.inflate(R.layout.drawer_fragment, container, false);
mDrawerLayout = (DrawerLayout) rootView.findViewById(R.id.drawer_layout);

但是之后 mDrawerLayout 为空,我猜是因为容器为空,所以我错过了什么?

布局 R.layout.drawer_fragment 不包含 ID 为 R.id.drawer_layout 的视图,所以是的,mDrawerLayout 将始终为空。如果您想访问 ID 为 drawer_layout 的视图,您需要从 Activity.

调用