【问题标题】:Binary XML file line #9: Error inflating class fragment二进制 XML 文件第 9 行:膨胀类片段时出错
【发布时间】:2011-08-23 14:12:41
【问题描述】:

我收到错误inflating fragment error while running calling an Activity which having fragments.

CheckList 是包含listview and Description fragments.的Activity,它的xml 代码是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent" android:layout_height="match_parent">

        <fragment class="com.uday.honeytest.CheckList$listview"
                android:id="@+id/titles" android:layout_weight="1"
                android:layout_width="0px" android:layout_height="match_parent" />

        <fragment class="com.uday.honeytest.CheckList$Description"
        android:id="@+id/details" android:layout_weight="1"
                android:layout_width="0px" android:layout_height="match_parent"
                android:background="?android:attr/detailsElementBackground" />

    </LinearLayout>

CheckList 类是:

public class CheckList extends Activity {

    static int position=0;
    static CheckList check;

    public void onCreate(Bundle saved){

        super.onCreate(saved);
        setContentView(R.layout.checklist);


    }

    public static class listview extends ListFragment {

            View mConverView;
            String[] items;



             @Override
                public void onActivityCreated(Bundle savedInstanceState) {
                    super.onActivityCreated(savedInstanceState);
             }

            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                xxxxxx..
                     return mConverView;
            }
        }

类似地,我在此下方有描述片段。

但我得到了

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.uday.honeytest/com.uday.honeytest.CheckList}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment

setContentView 行出错。

我的描述片段类是:已编辑:

public static class Description extends Fragment {

        View mConverView;
        String details[];

        public static Description getInstance() {
            Description desc=new Description();
            return desc;

        }

         @Override
            public void onActivityCreated(Bundle savedInstanceState) {
                super.onActivityCreated(savedInstanceState);
         }


        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {


            TextView textview=new TextView(getActivity());
            textview.setLayoutParams(new LayoutParams(MarginLayoutParams.WRAP_CONTENT, MarginLayoutParams.WRAP_CONTENT));



            return textview;
        }
    }

这里做错了什么??

谢谢

【问题讨论】:

  • 你真的应该发布你的描述类代码。
  • 检查我的描述片段类已编辑:仅在 CheckList Activity 中。
  • 我从这里尝试了这个例子:vogella.com/articles/Android/article.html#fragments,我遇到了两个问题: - 我需要使用android.support.v4.app 而不是android.app.Fragment,因为我在 API 级别 8 下运行(Android 引入Android 3.0 中的片段(API 级别 11)) - 片段声明中的 android:layout_marginTop="?android:attr/actionBarSize" 与 API 级别 8 不兼容 - 请参阅 stackoverflow.com/questions/7760817/…

标签: android android-3.0-honeycomb android-fragments


【解决方案1】:

您的活动清单应扩展 FragmentActivity 。

【讨论】:

  • 谢谢!你为我节省了很多时间!
【解决方案2】:

&lt;fragment class="com.uday.honeytest.CheckList$Description" 是它得到错误的地方。

您确定正确输入了完全限定的类名吗?

您是否尝试过清理您的项目/修复项目属性?

我在那个类中没有看到任何名为 Description 的东西,这可能是问题所在。

希望这会有所帮助。

【讨论】:

    【解决方案3】:

    我刚刚删除了 listview fragment 中的 onCreateView 代码 which contains inflating xml 并进入 onActivityCreated 方法,它的工作原理。

    The reason may be:
    

    ListFragment 有一个由单个列表视图组成的默认布局。但是,如果您愿意,您可以通过从 onCreateView(LayoutInflater, ViewGroup, Bundle) 返回您自己的视图层次结构来自定义片段布局。为此,您的视图层次结构必须包含一个 ID 为“@android:id/list”的 ListView 对象(如果它在代码中,则为列表)

    所以我的列表视图扩展了 ListFragment 而不是在 xml 中包含列表,我们可以直接在 onActivityCreated 中设置ListAdapter 而不是在 onCreateView 中。

    【讨论】:

    • 您可能不需要这样做。我注意到在您的示例中,您的 onCreateView 方法上方没有 @Override 指令。它被调用了吗?
    • 这有点误导,因为您一定已经接受了@spin.goran 的回答
    猜你喜欢
    • 1970-01-01
    • 2014-05-04
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    相关资源
    最近更新 更多