2013-06-26 27 views
4

我想實現一個簡單的列表/詳細視圖如圖1 here但我得到一個「你的內容必須有一個ListView的id屬性是'android.R.id.list'「運行時異常。這個錯誤似乎已經被詳細討論過,如果結果的數量是多少,但大多數答案似乎是關於擴展ListActivity,而我擴展了ListFragment。到目前爲止,我沒有運氣來修復它。基底活動是:ListFragment給ListView的id屬性是'android.R.id.list'

SpeciesListActivity.java

public class SpeciesListActivity extends MenuItemActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_species_list); 

     // Check if the species_detail view is available. If it is 
     // we're running both fragments together. 
     if (findViewById(R.id.species_detail) != null) { 
     } 
    } 
} 

所述片段通過其佈局文件包括:

activity_species_list.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:baselineAligned="false" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <fragment android:name="com.example.fragments.SpeciesListFragment" 
      android:id="@+id/species_list" 
      android:layout_weight="1" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" /> 
    <fragment android:name="net.gamebreeder.fragments.SpeciesDetailFragment" 
      android:id="@+id/species_detail" 
      android:layout_weight="1" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" /> 
</LinearLayout> 

這是在兩個窗格佈局,除了第二個窗格外,單個窗格是相同的不包括nt(id species_detail)。

有問題的片段:

SpeciesListFragment.java

public class SpeciesListFragment extends ListFragment { 

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

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_species_list, container, false); 
    } 
} 

和碎片的佈局:

fragment_species_list.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="8dp" 
    android:paddingRight="8dp"> 

    <ListView android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:drawSelectorOnTop="false"/> 

    <TextView android:id="@id/android:empty" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="@string/empty_string"/> 

</LinearLayout> 

我試圖改變SpeciesListFragment不重寫onCreateView(),迫使它使用默認值,根據docs因爲:

注意:如果您的片段是ListFragment的子類,默認實現返回一個ListView來自onCreateView(),所以你不需要實現它。

但我仍然得到: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

一個問題,我發現建議我確保我的主要活動延伸FragmentActivity而這正是MenuItemActivity是:

public class MenuItemActivity extends FragmentActivity { 

它基本上只是一個包裝,所以我不必在每個活動中手動創建菜單。

任何幫助將不勝感激 - 我希望我只是想念一件簡單的事情。

編輯2013年6月27日

我試過在fragment_species_list.xml格式化ID既@id/android:list@android:id/list但既不工作。該文檔說使用@id/android:list,但我讀過的其他問題似乎表明,兩者都應該工作,但我得到這兩個錯誤。我也試過@+id/list

我也改變了基於下面的答案如下:

activity_species_list.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:baselineAligned="false" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <fragment class="com.example.fragments.SpeciesListFragment" 
      android:id="@+id/species_list" 
      android:layout_weight="1" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" /> 
    <fragment class="net.gamebreeder.fragments.SpeciesDetailFragment" 
      android:id="@+id/species_detail" 
      android:layout_weight="1" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" /> 
</LinearLayout> 

回答

2

我不知道究竟是什麼造成這種情況,但我設法通過刪除最初使用ADT eclipse模板創建的所有舊片段,活動和佈局文件來解決它。

我基本上重寫了一切,從頭開始,一次一件,以確保一切正確調用。我懷疑這是因爲所有引用都沒有在編譯後的代碼中更新,儘管清理了所有編譯過的代碼多次(手動和使用eclipse項目清理工具)。

-3

檢查烏爾fragment_species_list.xml,有

<ListView android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:drawSelectorOnTop="false"/> 

在烏拉圭回合的ListView ,該id應該是android:id =「@ + id/list」

+0

這次也沒有運氣,同樣的錯誤。 – cssp

+0

在我的activity_species_list.xml中做了兩處修改 – user2362893

+0

我只是添加了一個答案,用這個替換了當前的xml文件 – user2362893

-1

您的fragment_species_list.xml應如下所示。你有 「@安卓/ ID:列表」 應爲 「@ ID /安卓列表」

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingLeft="8dp" 
android:paddingRight="8dp"> 

<ListView android:id="@id/android:list" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:drawSelectorOnTop="false"/> 

<TextView android:id="@id/android:empty" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:text="@string/empty_string"/> 

</LinearLayout> 
+0

我忘了將它添加到我的問題,但我已經嘗試過。我剛剛嘗試過,但仍然無法正常工作。 – cssp

+0

嗯,你是否嘗試在onCreateView for SpeciesListFragment中添加一個斷點以確保它被調用? – user1634451

+0

我對你的實現感到困惑。看起來像你想要擁有手機和平板電腦佈局。如果是這種情況,你應該有兩個fragment_species_list.xml的手機和一個用於平板電腦 – user1634451

-1

只需更換該XML文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:baselineAligned="false" 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<fragment class="com.example.fragments.SpeciesListFragment" 
     android:id="@+id/species_list" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" /> 
<fragment class="net.gamebreeder.fragments.SpeciesDetailFragment" 
     android:id="@+id/species_detail" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" /> 
</LinearLayout> 
+0

只需使用此代碼替換您的activity_species_list.xml – user2362893

+0

我也進行了此更改,但沒有運氣。我仍然遇到同樣的錯誤。我也嘗試過將它與另一個將ID更改爲「@ + id/list」的建議結合使用,但這也不起作用。 – cssp

-2

這可能是因爲您正在使用

<ListView android:id="@android:id/list" 

嘗試給

<ListView android:id="@+id/list" 
+1

我也試過了,按照user2362893的評論 – cssp

+0

哦!可能只是乾淨的構建您的項目,以查看任何.xml文件中的任何錯誤。這有時發生在我身上,通常原因是xml中的一些可怕的錯誤。 – Prachi

+0

自從開始得到這個錯誤之後,我在每次編譯之前強制清理項目,並且XML文件沒有顯示任何錯誤:( – cssp

7

添加這包括對您的列表佈局:

<include layout="@android:layout/list_content" /> 
<ListView 
    android:id="@+id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/white" /> 

這個工作對我....

文檔在Android網站:http://developer.android.com/reference/android/app/ListFragment.html

公共查看onCreateView(LayoutInflater吹氣, ViewGroup container,Bundle savedInstanceState)

在API級別11中添加提供默認實現以返回 簡單列表視圖。子類可以覆蓋以替換它們自己的 佈局。如果這樣做,返回的視圖層次結構必須具有ListView ,其ID是android.R.id.list並且可以有選擇地具有兄弟視圖 id android.R.id.empty當列表是空。

如果你重寫此方法與自己的自定義內容, 考慮在你的佈局 文件的標準佈局list_content,讓你繼續保留所有 ListFragment的標準行爲。特別是,這是目前唯一能夠顯示內置不確定進度狀態的方法。

相關問題