ListFragment给出ID属性为'android.R.id.list'的ListView

ListFragment给出ID属性为'android.R.id.list'的ListView

问题描述:

我正在尝试实现如图1所示的简单列表/详细视图此处,但我收到您的内容必须具有其id属性为'android.R.id.list'的ListView"的运行时异常.如果结果量不胜枚举,似乎已经详细讨论了该错误,但是大多数答案似乎都是关于扩展ListActivity的,而我正在扩展ListFragment.到目前为止,我还没有运气修复它.基本活动是:

I'm trying to implement a simple list/detail view as in figure 1 here but I'm getting a "Your content must have a ListView whose id attribute is 'android.R.id.list'" runtime exception. This error seems to have been discussed at length if the amount of results is anything to go by, but most of the answers seem to be about extending ListActivity whereas I'm extending ListFragment. So far I've had no luck in getting it fixed. The base activity is:

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) {
        }
    }
}

通过其布局文件包含片段:

The fragments are included via its layout file:

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).

This is the two pane layout, the single pane one is the same except for the second fragment (id species_detail) not being included.

有问题的片段

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>

根据文档:

注意:如果您的片段是ListFragment的子类,则默认实现从onCreateView()返回ListView,因此您无需实现它.

Note: If your fragment is a subclass of ListFragment, the default implementation returns a ListView from onCreateView(), so you don't need to implement it.

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

我发现的一个问题建议我确保在主活动中扩展FragmentActivity,而这正是MenuItemActivity的含义:

One question I found suggested I ensure that I'm extending FragmentActivity in the main activity and that's exactly what MenuItemActivity is:

public class MenuItemActivity extends FragmentActivity {

基本上,这只是一个包装,所以我不必在每个活动中手动创建菜单.

It's basically just a wrapper so I don't have to manually create the menu in every activity.

任何帮助将不胜感激-我希望我只是缺少一些简单的东西.

Any help would be appreciated - I'm hoping I'm just missing something simple.

编辑2013-06-27

我尝试将fragment_species_list.xml中的ID格式化为@id/android:list@android:id/list,但均无效.文档说要使用@id/android:list,但是我读过的其他问题似乎都建议两者都可以工作,但是我都遇到了错误.我也尝试过@+id/list.

I've tried formatting the id in the fragment_species_list.xml as both @id/android:list and @android:id/list but neither work. The docs say to use @id/android:list but the other questions I've read seem to suggest that both should work but I get the error with both. I've tried with @+id/list as well.

我还根据以下答案更改了以下内容:

I've also changed the following based on the answers below:

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>

我不确定到底是什么引起的,但是我设法通过删除所有最初由ADT创建的旧片段,活动和布局文件来解决此问题蚀模板.

I'm not sure what exactly caused this but I managed to solve it by deleting all the old fragment, activity and layout files which were originally created with an ADT eclipse template.

我基本上从头开始重新编写了所有内容,一次一次,以确保正确调用所有内容.我怀疑这仅仅是由于尽管多次清除了所有编译代码(无论是手动还是使用eclipse项目清理工具),所有引用都没有在编译代码中进行更新.

I basically re-wrote everything from scratch, one piece at a time to ensure everything is getting called correctly. I suspect that this was simply due to all references not being updated in the compiled code despite cleaning out all the compiled code numerous times (both manually and with the eclipse project clean tool).