如何使用片段标签包含一个布局文件?

4

我无法成功生成一个名为activity_item_list.xml的Android布局文件,其内容如下:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/item_list"
    android:name="xxx.xxx.xxx.ItemListFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    tools:context=".ItemListActivity"
    tools:layout="@android:layout/list_content" />

好的,它会崩溃。以下是崩溃日志:

android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>

我会在这里列出所有步骤:
  1. 在eclipse中,通过向导新建一个项目并选择主/细节流程

  2. 向导完成后,我得到了4个布局xml文件:activity_item_detail.xml、activity_item_twopane.xml、activity_item_list.xml和fragment_item_detail.xml

  3. 让我们修改activity_item_twopane.xml。我想要重用一些布局。

原始的activity_item_twopane.xml如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:baselineAligned="false"
    android:divider="?android:attr/dividerHorizontal"
    android:orientation="horizontal"
    android:showDividers="middle"
    tools:context=".ItemListActivity" >

    <fragment
        android:id="@+id/item_list"
        android:name="xxx.xxx.xxx.ItemListFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        tools:layout="@android:layout/list_content" />

    <FrameLayout
        android:id="@+id/item_detail_container"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3" />
</LinearLayout>

修改后的文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:baselineAligned="false"
    android:divider="?android:attr/dividerHorizontal"
    android:orientation="horizontal"
    android:showDividers="middle"
    tools:context=".ItemListActivity" >

    <include
        layout="@layout/activity_item_list"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <include
        layout="@layout/activity_item_detail"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"/>
</LinearLayout>

现在第二个include块能够正常工作,但是第一个块会导致崩溃。
activity_item_detail.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/item_detail_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ItemDetailActivity"
    tools:ignore="MergeRootFrame" />

我试图修改activity_item_list.xml,如下所示,但仍然崩溃...
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment
        android:id="@+id/item_list"
        android:name="xxx.xxx.xxx.ItemListFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        tools:context=".ItemListActivity"
        tools:layout="@android:layout/list_content" />

</FrameLayout>

我不知道为什么安卓无法支持一个片段。谁能告诉我呢?谢谢!

你如何在布局中使用它? - waqaslam
你能把activity_item_detail的XML粘贴过来吗? - waqaslam
你的类路径在片段中定义正确吗? - waqaslam
附上activity_item_detail.xml。 - paranoia
1个回答

1

请问能否提供更多上下文,例如您是否尝试单独填充一个片段?如果是这样的话,据我所知,它不会起作用。它必须被嵌入到另一个布局中。 您可以使用以下链接了解有关使用片段的更多信息: http://developer.android.com/guide/components/fragments.html

希望能帮到您


你写道第一个文件的名称是“activity_item_detial.xml”。这里有个拼写错误:应该是“detail”而不是“detial”。如果在你的实际代码中也是这样,它将无法运行。请让我知道是否已经修复了这个问题。 - Egis
很抱歉,我的帖子中有一个打字错误。不是在代码中...实际上,文件名是由向导生成的,我从未更改过它。 - paranoia

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接