paranoia
paranoia

Reputation: 41

how to <include> a layout file with fragment tag?

i failed to a android layout file activity_item_list.xml, which content is list below:

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

Well, it will crash. crash log is below:

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

I will list all steps here:

  1. In eclipse, new a project via wizard and select master/detail flow

  2. after wizard finished, i got 4 layout xmls generated by wizard: activity_item_detail.xml, activity_item_twopane.xml, activity_item_list.xml and fragment_item_detail.xml

  3. let's modify activity_item_twopane.xml. i want to reuse some layouts.

The orignal 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>

the modified file:

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

Now, the 2nd include-block works well. But the 1st one will cause crash.

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

And I had tried to modify activity_item_list.xml as below, still crash....

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

i do not know why android can not support a fragment. who can tell me. thank you!

Upvotes: 4

Views: 10657

Answers (1)

Egis
Egis

Reputation: 879

can you please provide more context, such as did you try to inflate a fragment on its own? If so, it won't work as far as I know. It must be embedded in another layout. You can use this to learn more about using fragments: http://developer.android.com/guide/components/fragments.html

Hope it helps

Upvotes: 1

Related Questions