Android 将FragmentTransaction与DialogFragment一起使用

Android 将FragmentTransaction与DialogFragment一起使用,android,android-3.0-honeycomb,android-fragments,Android,Android 3.0 Honeycomb,Android Fragments,因此,我创建了一个DialogFragment,通过这个 现在它已经启动,并且在弹出窗口中进行用户交互后,我想在这个对话框中插入另一个片段。我试图通过FragmentTransaction.add()实现这一点,在这里我给它这个布局中一个容器的id。在这一点上,我得到: java.lang.IllegalArgumentException: No view found for id 0x7f09013f for fragment <fragmentClassThatIWasPushingI

因此,我创建了一个DialogFragment,通过这个

现在它已经启动,并且在弹出窗口中进行用户交互后,我想在这个对话框中插入另一个片段。我试图通过FragmentTransaction.add()实现这一点,在这里我给它这个布局中一个容器的id。在这一点上,我得到:

java.lang.IllegalArgumentException: No view found for id 0x7f09013f for fragment <fragmentClassThatIWasPushingIn>
java.lang.IllegalArgumentException:找不到片段id 0x7f09013f的视图
作为一个快速测试,我尝试将它推到一个容器id上,而不是在对话框中,而是在主支持活动中,这样做很好

DialogFragments及其容器ID是否存在不允许碎片事务的情况

作为权宜之计,我已经告诉我的事务隐藏当前的DialogFragment并显示这个新的片段,但是动画/显示有点不协调,所以我真的想解决这个问题


感谢当
对话框片段
显示为
对话框
时,它在容器视图中实际上不是真正的
片段
。它是一个无容器的
片段
,基本上是
对话框
的包装


因此,不能在
片段对话框
中显示
片段
。如果您真的想这样做,我认为最好的方法是创建一个新的
活动
样式为
对话框
,然后您也可以添加
片段。

实际上有一个容器,您可以在onCreateView方法中看到。您可以使用容器创建视图

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle icicle) {
    Log.d(TAG, "onCreateView");
    View v = inflater
            .inflate(R.layout.move_folder_dialog, container, false);
碎片管理器似乎无法获取容器


这可能是一个bug吗?

alexanderblom说得对,
DialogFragment
充当一个对话框,但是可以使用
setShowsDialog(false)将其作为一个片段

最后,以下几点对我起了作用:

文件:res/layout/wifidirect\u dialog\u wifidirect:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/dialog_wifidirect_layout">

    <LinearLayout
        android:id="@+id/frag_container"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:orientation="vertical" >

            <!-- This is replaced during runtime -->
            <RelativeLayout
                android:id="@+id/frag_list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="top" >
            </RelativeLayout>
    </LinearLayout>

    <!-- The Cancel Button -->
    <View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:layout_marginBottom="0dp"
    android:background="?android:attr/dividerVertical" />
    <Button
         android:id="@+id/dialog_wifidirect_cancel"
         style="?android:attr/buttonBarButtonStyle"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="@string/cancel"/>         
</LinearLayout>

显示带有commitAllowingStateLoss()的DialogFragment

对话框fragment.show(FragmentManager, 字符串标记)在允许状态丢失时没有显示对话框的选项。这有点奇怪和不一致,因为有一个DialogFragment.dismissAllowingStateLoss()

但您始终可以手动提交DialogFragment事务,或创建如下帮助器方法:

public static void showDialogAllowingStateLoss(FragmentManager fragmentManager, DialogFragment dialogFragment, String tag) {
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.add(dialogFragment, tag);
ft.commitAllowingStateLoss();
}

这是正确的。但是,可以使用setShowsDialog(false);,强制DialogFragment充当片段;。检查我的答案。
public static void showDialogAllowingStateLoss(FragmentManager fragmentManager, DialogFragment dialogFragment, String tag) {
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.add(dialogFragment, tag);
ft.commitAllowingStateLoss();
}