我正在尝试实现一个简单的列表/详细视图,如图 1 所示,但我得到一个“您的内容必须有一个 id 属性为 'android.R.id.list' 的 ListView”运行时异常。如果结果的数量可以通过,这个错误似乎已经被详细讨论过,但大多数答案似乎是关于扩展 ListActivity 而我正在扩展 ListFragment。到目前为止,我还没有成功修复它。基本活动是:
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>
这是双窗格布局,单窗格一个是相同的,除了不包括第二个片段(id species_detail)。
有问题的片段:
物种列表片段.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() 以强制它使用默认值,因为根据文档:
注意:如果你的fragment是ListFragment的子类,默认实现会从onCreateView()返回一个ListView,所以不需要实现。
但我仍然得到:
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-06-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>