二进制 XML 文件第 9 行:膨胀类片段时出错

发布于 2024-11-30 18:10:13 字数 2830 浏览 7 评论 0 原文

在运行调用具有片段的 Activity 时出现错误膨胀片段错误。

CheckList 是包含 listview 和描述片段的 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;
            }
        }

类似我在下面有描述片段。

但在 setContentView 行出现

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

错误。

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

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;
        }
    }

这里做错了什么?

谢谢

Am getting Error inflating fragment error while running calling an Activity which having fragments.

CheckList is Activity which contains listview and Description fragments.and its xml code is:

<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 class is:

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;
            }
        }

similarly i have Description fragment below this .

But am getting

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

error at setContentView line.

and my Description fragment class is: Edited:

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;
        }
    }

What am doing wrong here??

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

避讳 2024-12-07 18:10:13

您的活动 CheckList 应扩展 FragmentActivity 。

Your activity CheckList should extend FragmentActivity .

春风十里 2024-12-07 18:10:13

是出现错误的地方。

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

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

我在该类中没有看到任何名为 Description 的内容,这可能是问题所在。

希望这有帮助。

<fragment class="com.uday.honeytest.CheckList$Description" is where it's getting the error.

Are you sure you have the fully qualified class name typed correctly?

Have you tried cleaning your project/fixing project properties?

I don't see anything named Description in that class, that may be the issue.

Hope this helps.

另类 2024-12-07 18:10:13

我刚刚删除了 listview 片段中包含膨胀 xml 的 onCreateView 代码,并移至 onActivityCreated 方法并使其正常工作。

The reason may be:

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

因此我的 listview 扩展了 ListFragment 并且不包含 xml 中的 list ,我们可以直接在 onActivityCreated 中 setListAdapter而不是在 onCreateView 中。

I just removed onCreateView code which contains inflating xml in listview fragmentand move into onActivityCreated method with that its working.

The reason may be:

ListFragment has a default layout that consists of a single list view. However, if you desire, you can customize the fragment layout by returning your own view hierarchy from onCreateView(LayoutInflater, ViewGroup, Bundle). To do this, your view hierarchy must contain a ListView object with the id "@android:id/list" (or list if it's in code)

So my listview extends ListFragment and not conatining list in xml , and directly we can setListAdapter in onActivityCreated rather than in onCreateView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文