我想實現一個簡單的列表/詳細視圖如圖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>
這次也沒有運氣,同樣的錯誤。 – cssp
在我的activity_species_list.xml中做了兩處修改 – user2362893
我只是添加了一個答案,用這個替換了當前的xml文件 – user2362893