错误:内容必须具有其属性为android.R.id.list的ListView

时间:2012-12-18 05:05:57

标签: android listview android-fragments

使用SherlockListFragment,我无法找到错误的位置。我已就这个问题阅读了几个问答,但没有一个解决方案似乎能解决我的问题。任何帮助将不胜感激。感谢。

public class BuyFragTab extends SherlockListFragment {

ArrayList<Bookinfo> bookArray;
ListView bookLV;
MyCustomAdapter adapter = null;

//defines the Arraylist Bookinfo record layout
//public class Bookinfo {
public String titles[] = new String[]{"Bk 1", "Bk 2", "Bk 3", "Bk 4", "Bk 5", "Bk 6", "Bk 7", "Bk 8"};
public String authors[] = new String[]{"Tom", "Dick", "Harry", "Jack", "James", "Skip", "Jim", "June"};
public String status[] = new String[]{"Open", "Open", "Open", "Open", "Closed", "Closed", "Closed", "Closed"};
public static ArrayList<String> bkRecs;

Context context;


@Override
public void onActivityCreated(Bundle savedInstanceState){

    System.out.println("onActivityCreated executed");

    bookArray = new ArrayList<Bookinfo>();
    for (int i=0; i<8; i++) {
        Bookinfo bkRecs = new Bookinfo(titles[i], authors[i], status[i]);
        bookArray.add(bkRecs);
        System.out.println("bookArray =" + titles[i]);
    }

    //adapter = new MyCustomAdapter(getActivity(), android.R.id.list,   bookArray);
    adapter = new MyCustomAdapter(getActivity(), R.layout.buy_tbfrag, bookArray);
    //adapter = new MyCustomAdapter();
    //ArrayAdapter<Bookinfo> adapter = new ArrayAdapter<Bookinfo>(getActivity().getBaseContext(),
    //      R.layout.buy_tbfrag_list, bookArray);
    bookLV.setAdapter(adapter);
    super.onActivityCreated(savedInstanceState);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.buy_tbfrag_list, container, false);

    //bookLV = (ListView)view.findViewById(R.id.list);
    //bookLV = getListView();
    bookLV = (ListView)view.findViewById(android.R.id.list);

    return view;
}
}

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<!-- Put a background to the fragment android:background="#FF0000" -->
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/titleSearch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Input Title Name" />

    <Button
        android:id="@+id/button1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Search" />

</LinearLayout>

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</ListView>  

<TextView
    android:id="@+id/empty"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="No Books Registered" />

</LinearLayout>

ListView xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
        android:id="@+id/titleName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

<TextView
        android:id="@+id/authorName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

<TextView
        android:id="@+id/bookStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

</LinearLayout>

我的Logcat:

12-18 16:01:21.549: D/AndroidRuntime(646): Shutting down VM
12-18 16:01:21.549: W/dalvikvm(646): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
12-18 16:01:21.589: E/AndroidRuntime(646): FATAL EXCEPTION: main
12-18 16:01:21.589: E/AndroidRuntime(646): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.skipster.BookBarter/com.skipster.BookBarter.BookBarterActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-18 16:01:21.589: E/AndroidRuntime(646):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-18 16:01:21.589: E/AndroidRuntime(646):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-18 16:01:21.589: E/AndroidRuntime(646):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-18 16:01:21.589: E/AndroidRuntime(646):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-18 16:01:21.589: E/AndroidRuntime(646):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 16:01:21.589: E/AndroidRuntime(646):  at android.os.Looper.loop(Looper.java:137)
12-18 16:01:21.589: E/AndroidRuntime(646):  at android.app.ActivityThread.main(ActivityThread.java:4340)
12-18 16:01:21.589: E/AndroidRuntime(646):  at java.lang.reflect.Method.invokeNative(Native Method)
12-18 16:01:21.589: E/AndroidRuntime(646):  at java.lang.reflect.Method.invoke(Method.java:511)
12-18 16:01:21.589: E/AndroidRuntime(646):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-18 16:01:21.589: E/AndroidRuntime(646):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-18 16:01:21.589: E/AndroidRuntime(646):  at dalvik.system.NativeStart.main(Native Method)
12-18 16:01:21.589: E/AndroidRuntime(646): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-18 16:01:21.589: E/AndroidRuntime(646):  at android.support.v4.app.ListFragment.ensureList(ListFragment.java:344)
12-18 16:01:21.589: E/AndroidRuntime(646):  at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:145)
12-18 16:01:21.589: E/AndroidRuntime(646):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:884)
12-18 16:01:21.589: E/AndroidRuntime(646):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
12-18 16:01:21.589: E/AndroidRuntime(646):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
12-18 16:01:21.589: E/AndroidRuntime(646):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
12-18 16:01:21.589: E/AndroidRuntime(646):  at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:505)
12-18 16:01:21.589: E/AndroidRuntime(646):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133)
12-18 16:01:21.589: E/AndroidRuntime(646):  at android.app.Activity.performStart(Activity.java:4475)
12-18 16:01:21.589: E/AndroidRuntime(646):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1928)
12-18 16:01:21.589: E/AndroidRuntime(646):  ... 11 more
12-18 16:06:21.879: I/Process(646): Sending signal. PID: 646 SIG: 9

3 个答案:

答案 0 :(得分:0)

替换

adapter = new MyCustomAdapter(getActivity(), R.layout.buy_tbfrag, bookArray);

adapter = new MyCustomAdapter(getActivity(), R.layout.list, bookArray);

答案 1 :(得分:0)

当您extending ListActivity时,无需初始化它,因为如果在布局中使用带有@android:id / empty ID的View,ListActivity将自动显示此View ListView为空,否则隐藏它。

  

setListAdapter(适配器);

相反

  

bookLV.setAdapter(适配器);

然后清理你的项目并运行它!

答案 2 :(得分:0)

变化

android:id="@+id/authorName"

android:id="@android:id/authorName"