为什么会出现这些错误?
04-10 17:07:57.880: E/AndroidRuntime(917): Process: com.example.programming_fragments, PID: 917
04-10 17:07:57.880: E/AndroidRuntime(917): java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-10 17:07:57.880: E/AndroidRuntime(917): at android.support.v4.app.ListFragment.ensureList(ListFragment.java:344)
04-10 17:07:57.880: E/AndroidRuntime(917): at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:145)
04-10 17:07:57.880: E/AndroidRuntime(917): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:925)
04-10 17:07:57.880: E/AndroidRuntime(917): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
04-10 17:07:57.880: E/AndroidRuntime(917): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-10 17:07:57.880: E/AndroidRuntime(917): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
04-10 17:07:57.880: E/AndroidRuntime(917): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
04-10 17:07:57.880: E/AndroidRuntime(917): at android.os.Handler.handleCallback(Handler.java:733)
04-10 17:07:57.880: E/AndroidRuntime(917): at android.os.Handler.dispatchMessage(Handler.java:95)
04-10 17:07:57.880: E/AndroidRuntime(917): at android.os.Looper.loop(Looper.java:136)
04-10 17:07:57.880: E/AndroidRuntime(917): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-10 17:07:57.880: E/AndroidRuntime(917): at java.lang.reflect.Method.invokeNative(Native Method)
04-10 17:07:57.880: E/AndroidRuntime(917): at java.lang.reflect.Method.invoke(Method.java:515)
04-10 17:07:57.880: E/AndroidRuntime(917): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-10 17:07:57.880: E/AndroidRuntime(917): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-10 17:07:57.880: E/AndroidRuntime(917): at dalvik.system.NativeStart.main(Native Method)
代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="schemas.android.com/apk/res/android"; android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffeeeeee" android:gravity="center" android:orientation="vertical" > <ListView android:id="@+id/listView1" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
答案 0 :(得分:0)
您的内容必须包含一个ListView,其id属性为android.R.id.list。所以转到包含listview的xml并添加id,使其看起来像:
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
当然,您可以拥有自定义列表,但我假设您正在使用基于您的问题的简单列表适配器,因此这应该适合您。
答案 1 :(得分:0)
问题在于您在 onCreateView ()中设置的布局。
如果您在自定义 ListFragment 中覆盖onCreateView,则需要对其中包含ListView的布局进行充气,其中id android:id =“@ android:id / list” 即可。
如果你真的想要一个不同于ListFragment中使用的默认布局,那么只覆盖onCreateView(),否则使用默认布局。
这就是ListFragment中的代码。您可以查看默认布局( R.layout.list_content ),看看是否真的需要覆盖此布局
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(com.android.internal.R.layout.list_content,
container, false);
}