我正在使用本教程中的材料设计导航抽屉

http://www.androidhive.info/2015/04/android-getting-started-with-material-design/

从第一个片段开始,我试图重定向到另一个片段,但是我的应用程序崩溃了,并显示错误,

错误

java.lang.IllegalArgumentException: No view found for id 0x7f0d0053 (info.androidhive.materialdesign:id/container_body) for fragment All_Products{41ca7008 #0 id=0x7f0d0053}


ShopFragment.java

rl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   All_Products tf = new All_Products();
                    Bundle bundle = new Bundle();
                    bundle.putString("catagory_name", "All");
                    tf.setArguments(bundle);
                    android.support.v4.app.FragmentManager fm = getFragmentManager();
                    android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
                    ft.replace(R.id.container_body, tf);
                    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    ft.addToBackStack(null);
                    ft.commit();
                }
            });


activity_main

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/container_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <include
                android:id="@+id/toolbar"
                layout="@layout/toolbar" />
        </LinearLayout>

        <FrameLayout
            android:id="@+id/container_body"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />


    </LinearLayout>


    <fragment
        android:id="@+id/fragment_navigation_drawer"
        android:name="info.androidhive.materialdesign.activity.FragmentDrawer"
        android:layout_width="@dimen/nav_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/fragment_navigation_drawer"
        tools:layout="@layout/fragment_navigation_drawer" />

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

最佳答案

我认为您应该替换此行

 android.support.v4.app.FragmentManager fm = getFragmentManager();


有了这个

 android.support.v4.app.FragmentManager fm = getActivity().getSupportFragmentManager();


因为首先要初始化不支持的片段管理器以支持片段管理器实例。
其次,当您在活动抽屉布局中定义container_body框架布局时,必须使用活动片段管理器替换它。

关于android - 使用Material design fragment 不会传递给另一个 fragment ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31875695/

10-11 22:43
查看更多