0

Working with SherlockListFragment, I can't find where my error is. I've read several Q/A's on this subject but none of the solutions seem to rectify my problem. Any help would be greatly appreciated. Thanks.

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 code:

<?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 code:

<?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>

My 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
Makyen
  • 31,849
  • 12
  • 86
  • 121
Skip
  • 35
  • 9
  • 2
    If the first layout is `buy_tbfrag_list.xml`, try cleaning your project. – Sam Dec 18 '12 at 05:09
  • thanks for responding, the following is my logcat: – Skip Dec 18 '12 at 22:22
  • Sorry for the delay, computer problems. I just posted the logcat. I clean the project but still getting same error. – Skip Dec 18 '12 at 22:26
  • Skip, I'm having a similar problem. Did you get to the bottom of your issue? Thanks in advance. Here's my question: http://stackoverflow.com/questions/14205540/listfragment-android-r-id-list-missing-runtimeexception – logray Jan 12 '13 at 16:24

3 Answers3

0

Replace

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

with

adapter = new MyCustomAdapter(getActivity(), R.layout.list, bookArray);
onkar
  • 4,427
  • 10
  • 52
  • 89
  • 1
    How does this address "Content must have a ListView whose id attribute android.R.id.list"? And how do you know there is a layout file named `list.xml`? – Sam Dec 18 '12 at 05:13
  • R.layout.buy_tbfrag has the android:id="@android:id/list" defined in the listview parameter. – Skip Dec 18 '12 at 22:35
0

When you extending ListActivity then there is no need to initialize it, because If you use a View with the @android:id/empty ID in your layout, ListActivity will automatically show this View if the ListView is empty and hide it otherwise.

setListAdapter(adapter);

Instead

bookLV.setAdapter(adapter);

and then clean your project and run it!

RobinHood
  • 10,897
  • 4
  • 48
  • 97
  • Thanks. Made the changes but still received the same error. Can you check the logcat just posted. – Skip Dec 18 '12 at 22:27
0

change

android:id="@+id/authorName"

to

android:id="@android:id/authorName"
user2983227
  • 25
  • 2
  • 8
  • Could you please [edit] in an explanation of why/how this code answers the question? Code-only answers are discouraged, because they are not as easy to learn from as code with an explanation. Without an explanation it takes considerably more time and effort to understand what was being done, the changes made to the code, or if the code is useful. The explanation is important both for people attempting to learn from the answer and those evaluating the answer to see if it is valid, or worth up voting. – Makyen Feb 24 '15 at 19:41
  • http://stackoverflow.com/questions/11050817/your-content-must-have-a-listview-whose-id-attribute-is-android-r-id-list – user2983227 Feb 25 '15 at 02:25
  • `android:id="@android:id/authorName"` is not `android:id="@android:id/list"`. So, I don't see how the question you link to: [Your content must have a ListView whose id attribute is 'android.R.id.list'](http://stackoverflow.com/questions/11050817/your-content-must-have-a-listview-whose-id-attribute-is-android-r-id-list) is relevant to *your* answer. – Makyen Feb 25 '15 at 04:47