2013-03-26 39 views
3

我沒有一個Android的佈局文件activity_item_list.xml,其內容如下列表:如何<include>帶有片段標籤的佈局文件?

<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個由嚮導生成的佈局xmls: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> 

現在,第二個包括塊效果很好。但第一個會導致崩潰。

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> 

我不知道爲什麼機器人不能支持一個片段。誰可以告訴我。謝謝!

+0

以及如何在您的佈局使用它嗎? – waqaslam 2013-03-26 08:28:35

+0

請看我的新帖子。謝謝! – paranoia 2013-03-26 09:26:42

+0

你也可以粘貼xml for activity_item_detail嗎? – waqaslam 2013-03-26 09:30:54

回答

1

請問您能否提供更多上下文,比如您是否嘗試自行膨脹片段?如果是這樣,就我所知,它不會工作。它必須嵌入到另一個佈局中。 你可以用它來了解更多關於使用片段: http://developer.android.com/guide/components/fragments.html

希望它可以幫助

+0

我剛剛編輯了我原來的帖子。謝謝! – paranoia 2013-03-26 09:07:12

+0

你已經寫了第一個文件的名字是「activity_item_detial.xml」。這裏有一個拼寫錯誤:應該是'detail'而不是'detial'。如果它在你的實際代碼中是一樣的,它將不會運行。請讓我知道,如果它修好了 – Egis 2013-03-27 19:14:36

+0

好,這是我的文章中的一個錯字。抱歉。不在代碼中......實際上,文件名是由嚮導生成的,我從來沒有改變過。 – paranoia 2013-03-29 01:28:36