【问题标题】:Using Material design Fragment is not passing to another fragment使用材料设计片段没有传递给另一个片段
【发布时间】:2015-08-07 20:19:49
【问题描述】:

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

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>

【问题讨论】:

  • 如果您从一个片段转到另一个片段,您应该使用 getChildFragmentManager() 吗?
  • 添加包含片段的 xml 文件,可能是您没有放入 id 标签,或者您有重复/未充分分配的 ID
  • @sirFunkenstine 已经尝试过使用 getchildfragmentmanager..但还是同样的错误
  • @Hughzi 你可以在给定的链接中看到我的整个结构..
  • @Hughzi 查看我更新的问题和错误

标签: android android-fragments


【解决方案1】:

我认为你应该替换这一行

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

有了这个

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

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

【讨论】:

    【解决方案2】:

    @Roman:供您参考

    你的错误是什么

    片段可从目标 API 应 >= 11 获得。 getFragmentManager() 用于 API>=14, 和getSupportFragmentManager() 将用于支持库,例如v4,它也支持旧API。所以如果你使用 AppCompatActivity 或 FragmentActivity 那么你应该使用getSupportFragmentManager() getFragmentManager().

    【讨论】:

      【解决方案3】:

      我建议您使用接口并从活动中调用片段,而不是从片段中调用另一个片段。带有片段的片段可能会导致长期引用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-12
        • 2020-05-31
        • 1970-01-01
        • 1970-01-01
        • 2019-02-02
        • 1970-01-01
        相关资源
        最近更新 更多