在fragmentdialog中膨胀片段时出错



我试图在片段对话框中显示一个已经创建的listviewfragment。我使用了下面问题的答案来做这件事,但它不起作用。

如何在DialogFragment 中显示现有ListFragment

当我试图打开碎片对话框时,我得到了一个Error inflating class fragment,应用程序崩溃了

以下是dialog_fragment_with_list_fragment布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
 <fragment
         android:id="@+id/flContent"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:padding = "10dp"
         class="com.OptimusApps.stayhealthy.AndroidXMLParsingActivity" />
</LinearLayout>

并不是androidxmlparsingactivity片段导致了它的失败,我已经用其他片段进行了尝试,但它们也不起作用

下面是我的对话片段类

public class BodyDialogue extends DialogFragment {
int mNum;
/**
 * Create a new instance of MyDialogFragment, providing "num"
 * as an argument.
 */
static BodyDialogue newInstance(int num) {
    BodyDialogue f = new BodyDialogue();
    // Supply num input as an argument.
    Bundle args = new Bundle();
    args.putInt("num", num);
    f.setArguments(args);
    return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mNum = getArguments().getInt("num");
    // Pick a style based on the num.
    int style = DialogFragment.STYLE_NORMAL, theme = 0;
    switch ((mNum-1)%6) {
        case 1: style = DialogFragment.STYLE_NO_TITLE; break;
        case 2: style = DialogFragment.STYLE_NO_FRAME; break;
        case 3: style = DialogFragment.STYLE_NO_INPUT; break;
        case 4: style = DialogFragment.STYLE_NORMAL; break;
        case 5: style = DialogFragment.STYLE_NORMAL; break;
        case 6: style = DialogFragment.STYLE_NO_TITLE; break;
        case 7: style = DialogFragment.STYLE_NO_FRAME; break;
        case 8: style = DialogFragment.STYLE_NORMAL; break;
    }
    switch ((mNum-1)%6) {
        case 4: theme = android.R.style.Theme_Holo; break;
        case 5: theme = android.R.style.Theme_Holo_Light_Dialog; break;
        case 6: theme = android.R.style.Theme_Holo_Light; break;
        case 7: theme = android.R.style.Theme_Holo_Light_Panel; break;
        case 8: theme = android.R.style.Theme_Holo_Light; break;
    }
    setStyle(style, theme);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.dialog_fragment_with_list_fragment, null);
    return view;
}
}

这就是我称对话片段的方式

public void onClick(View v) {
                BodyDialogue  dialogFragment = BodyDialogue.newInstance(1);
                 dialogFragment .setRetainInstance(true);
                 dialogFragment .show(getFragmentManager(), "bodydialogue");
            }

这就是logcat 中的原因

08-17 19:43:15.702: E/AndroidRuntime(3605): Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f0a0031, tag null, or parent id 0x0 with another fragment for com.OptimusApps.stayhealthy.AndroidXMLParsingActivity
08-17 19:43:15.702: E/AndroidRuntime(3605):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)

您必须为每个膨胀的片段使用另一个视图!

例如,首次充气使用:

View view = inflater.inflate(R.layout.dialog_fragment_with_list_fragment, null);

然后使用另一个名称来进行相同布局的第二次充气:

View secondview = inflater.inflate(R.layout.dialog_fragment_with_list_fragment, null);

最新更新