我正在尝试实现一个简单的列表/详细信息视图,如图1
here,但我得到一个“你的内容必须有一个ListView,其id属性是’android.R.id.list’”运行时异常.如果结果的数量可以通过,似乎已经详细讨论了这个错误,但大多数答案似乎是关于扩展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)之外,单个窗格是相同的.
有问题的片段:
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:
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:您的内容必须具有一个ListView,其id属性为’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>
最佳答案 将此包含添加到列表布局中:
<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
container, Bundle savedInstanceState)Added in API level 11 Provide default implementation to return a
simple list view. Subclasses can override to replace with their own
layout. If doing so, the returned view hierarchy must have a ListView
whose id is android.R.id.list and can optionally have a sibling view
id android.R.id.empty that is to be shown when the list is empty.If you are overriding this method with your own custom content,
consider including the standard layout list_content in your layout
file, so that you continue to retain all of the standard behavior of
ListFragment. In particular, this is currently the only way to have
the built-in indeterminant progress state be shown.