3

我正在尝试实现一个简单的列表/详细视图,如图 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>
4

6 回答 6

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

public View onCreateView (LayoutInflater inflater, ViewGroup 容器, Bundle savedInstanceState)

在 API 级别 11 中添加 提供默认实现以返回简单的列表视图。子类可以覆盖以替换为自己的布局。如果这样做,返回的视图层次结构必须有一个 ID 为 android.R.id.list 的 ListView,并且可以选择在列表为空时显示一个兄弟视图 id android.R.id.empty。

如果您使用自己的自定义内容覆盖此方法,请考虑在布局 文件中包含标准布局 list_content,以便继续保留 ListFragment 的所有标准行为。特别是,这是目前显示内置不确定进度状态的唯一方法。

于 2013-09-13T22:41:54.910 回答
2

我不确定究竟是什么原因造成的,但我设法通过删除最初使用 ADT eclipse 模板创建的所有旧片段、活动和布局文件来解决它。

我基本上从头开始重写所有内容,一次一个,以确保正确调用所有内容。我怀疑这仅仅是因为尽管多次清理了所有编译代码(手动和使用 eclipse 项目清理工具),但编译代码中的所有引用都没有更新。

于 2013-06-27T22:21:01.277 回答
-2

您的 fragment_species_list.xml 应如下所示。你有“@android/id:list”应该是“@id/android:list”

<?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> 
于 2013-06-26T18:25:08.230 回答
-2

只需替换此 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>
于 2013-06-27T09:46:14.143 回答
-2

这可能是因为您正在使用

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

尝试给予

 <ListView android:id="@+id/list" 
于 2013-06-27T10:06:15.850 回答
-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"/>

在你的列表视图中,id 应该是 android:id="@+id/list"

于 2013-06-26T18:22:22.830 回答