繁体   English   中英

错误放大DialogFragment中的类片段

[英]Error inflating Class fragment in DialogFragment

我正在尝试创建一个内部带有listview的dialogfragment,我使用了该问题的可接受答案

如何在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;

}
}

这就是我所说的dialogfragment

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)

但是,当我尝试打开片段对话框并且应用崩溃时,我得到了一个错误夸大类片段

之所以发生这种情况,是因为您使用了一个已经包含另一个片段(通过fragment标签)的布局作为BodyDialogue片段的视图。 这将失败,因为不允许从xml布局中夸大嵌套的片段 ,因为嵌套片段的指南已经提到:

注意:当布局包含<fragment>时,不能将其充气为一个片段。 仅当动态将片段添加到片段时,才支持嵌套片段。

因此,如果您想在BodyDialogue对话框片段中嵌入AndroidXMLParsingActivity (为片段命名糟糕的名称),请使用getChildFragmentManager()在同一onCreateView回调中的代码中进行操作。

我用这个简单的方法解决了我的问题

请参考这个答案

https://stackoverflow.com/a/14966061/963591

我的代码使用上面的答案

@Override
public void onClick(View v) {

// TODO Auto-generated method
                                        // stub

FragmentTransaction ft2 = getFragmentManager().beginTransaction();
                                        ft2.remove(getFragmentManager().findFragmentByTag("one"));
                                        ft2.commit();

dialog.dismiss();

}

我的xml

<fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.MapFragment"
            android:layout_width="match_parent"
            android:layout_height="220dip"
            android:tag="one"
            android:layout_below="@+id/txtTargetStreet_hint"
            android:layout_margin="20dip" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM