0

使用 SherlockListFragment,我找不到我的错误在哪里。我已经阅读了几个关于这个主题的 Q/A,但似乎没有一个解决方案可以解决我的问题。任何帮助将不胜感激。谢谢。

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>

列表视图 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>

我的日志猫:

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
4

3 回答 3

0

代替

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

adapter = new MyCustomAdapter(getActivity(), R.layout.list, bookArray);
于 2012-12-18T05:11:33.000 回答
0

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

setListAdapter(适配器);

反而

bookLV.setAdapter(适配器);

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

于 2012-12-18T05:21:44.083 回答
0

改变

android:id="@+id/authorName"

android:id="@android:id/authorName"
于 2015-02-24T19:13:40.313 回答