【问题标题】:Can I put one fragment into another fragment which is in FragmentActivity我可以将一个片段放入 FragmentActivity 中的另一个片段中吗
【发布时间】:2012-10-19 14:24:05
【问题描述】:

我有一个 FragmentActivity 这个活动有一个 Fragment 这个 Fragment 由 xml 中定义的小部件和片段组成。

当我开始活动时它崩溃了

10-19 13:27:57.470: E/AndroidRuntime(16701): android.view.InflateException: Binary XML file line #43: Error inflating class fragment

是否可以嵌套 Fragment,还是只能在 FragmentActivity 中放置 Fragment?

编辑: 这是位于 FragmentActivity ViewPager 中的片段的 xml

膨胀时程序在此文件中崩溃

        <com.ssbs.sw.SWE.widget.SeekBarWidget
            android:id="@+id/pa_outlet_characteristics_trading_stocks"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="999999"
            android:text="@string/label_outlet_characteristics_trading_stocks" />

        <fragment
            android:id="@+id/pa_outlet_characteristics_spinners"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            class="com.ssbs.sw.SWE.pure.outlet.pages.characteristics.view.components.SpinnersFragment"
            tools:layout="@layout/pf_characteristics_custperday_spinner" />

        <com.ssbs.sw.SWE.widget.SpinnerWidget
            android:id="@+id/pa_outlet_characteristics_proximity_factor"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?spinerBackground"
            widget:label="@string/label_outlet_characteristics_proximity_factor"
            widget:popupDialogTheme="@style/ActivityDialog"
            widget:spinnerTemplate="@layout/widget_spinner" />

更新

从 11 版本开始在支持库中添加了对嵌套片段的支持

支持包,修订版 11(2012 年 11 月)
v4 支持库的更改:
用户界面
添加了对嵌套片段类的支持。
添加了对 FragmentManager 调试的改进。
.......

Nested Fragments documentation

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

    使用我不知道的支持 API,但通常无法创建嵌套片段:您可以从另一个片段创建片段,但该片段将仅与创建原始片段的 Activity 通信(如果不清楚,告诉我)

    编辑:

    至于为什么会崩溃,如果不是嵌套的fragment,我就不知道了。也许您应该尝试创建它并以编程方式将其添加到您的布局中

    【讨论】:

    • 你是什么意思“片段将只与创建原始片段的活动”通信?
    • 我的意思是你不能将另一个片段的实例传递给你的片段,因为你的 Bundle 参数不会记住它(它不是 Parcelable),所以例如,如果你转动你的屏幕应用程序将崩溃,因为它必须重建您的片段,并且此片段不会引用您要与之通信的片段(很复杂,是的!)
    • 而且你的活动不是这样,因为最后,即使你从另一个片段中创建你的片段,它是真正创建它的原始活动(如果我错了,有人会阻止我,我我不是 100% 肯定我说的话)。因此,您的片段始终具有对创建它的活动的引用。
    • 问题是我什至不能创建这个片段/
    • 正如我在编辑中所说,也许您应该尝试创建它并以编程方式将其添加到您的布局中
    【解决方案2】:

    不允许嵌套片段。 (取决于版本)。

    编辑 - 以前不支持它,但它是从版本 11 中添加的,如上面的帖子所述:https://developer.android.com/about/versions/android-4.2.html#NestedFragments

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多