【问题标题】:Error inflating Class fragment in DialogFragment在 DialogFragment 中膨胀类片段时出错
【发布时间】:2013-08-17 23:28:06
【问题描述】:

我正在尝试创建一个内部带有列表视图的对话框片段,并且我使用了这个问题中接受的答案来做到这一点

How to display an existing ListFragment in a DialogFragment

但是当我尝试打开片段对话框并且应用程序崩溃时,我收到了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)

【问题讨论】:

  • 是的,因为如果我只是调用它来替换片段,它看起来很好。就像我说的,它与那个片段无关,我在其他片段上测试过,它们也不起作用,我添加了我的 logcat,我想你可能会在那里找到问题的解决方案
  • 请不要在您的问题标题前加上Android或任何其他标签,底部的标签绰绰有余。

标签: java android android-fragments android-fragmentactivity android-dialogfragment


【解决方案1】:

但是当我尝试打开时,我得到一个错误膨胀类片段 片段对话框和应用程序崩溃

发生这种情况是因为您将BodyDialogue 片段的视图用作已经包含另一个片段的布局(通过fragment 标记)。这将失败,因为不允许从 xml 布局中扩展嵌套片段,正如the guide for the nested fragments 已经提到的那样:

注意:当布局包含 时,您不能将布局扩展为片段。只有在动态添加到片段时才支持嵌套片段。

因此,如果您想在BodyDialogue 对话框片段中嵌入AndroidXMLParsingActivity(一个片段的糟糕命名顺便说一句),那么请使用getChildFragmentManager() 在同一onCreateView 回调中的代码中执行此操作。

【讨论】:

  • 你有这样的例子吗?
  • @CamConnor 如果您还没有自己制作,请查看gist.github.com/luksprog/6265896
  • +1“嵌套片段指南已经提到”......似乎你是唯一一个读过它的人:) 在我读到这篇文章之前,我一直在与 xmls+nestedfrags 战斗。跨度>
【解决方案2】:

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

请参考这个答案

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" />

【讨论】:

    猜你喜欢
    • 2011-09-19
    • 2013-05-28
    • 1970-01-01
    • 2014-05-19
    • 2023-03-05
    相关资源
    最近更新 更多