2012-12-18 50 views
0

使用SherlockListFragment,我找不到我的错误在哪里。我已经阅读了关于这个主题的几个Q/A,但没有一个解决方案似乎纠正了我的问题。任何帮助将不胜感激。谢谢。错误:内容必须有一个ListView的id属性android.R.id.list

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 
+1

安置自己的日志太 –

+2

如果第一个布局'buy_tbfrag_list.xml',请尝试清洁您的项目。 – Sam

+0

感谢您的回复,以下是我的logcat: – Skip

回答

0

更换

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

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

这个地址如何“内容必须有一个ListView的id属性android.R.id.list “?你怎么知道有一个名为'list.xml'的布局文件? – Sam

+0

R.layout.buy_tbfrag在listview参数中定义了android:id =“@ android:id/list”。 – Skip

0

当你extending ListActivity那么就没有必要对它进行初始化,因为如果您使用与@android一种观点:在你的布局ID /空ID,ListActivity会自动显示该查看ListView是否为空,否则将其隐藏。

setListAdapter(adapter);

相反

bookLV.setAdapter(adapter);

,然后清理项目并运行它!

+0

谢谢。进行了更改但仍收到相同的错误。你可以检查刚刚发布的logcat吗? – Skip

0

变化

android:id="@+id/authorName" 

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

你能否请[编辑]解释为什么/如何代码回答这个问题?不提供代码的答案是不鼓励的,因为它们不像代码解释那样容易学习。没有解释,需要花费更多的时间和精力去理解正在做什么,对代码所做的更改,或者代码是否有用。对于试图从答案中学习的人以及评估答案以查看答案是否有效或值得投票的人来说,这种解释非常重要。 – Makyen

+0

http://stackoverflow.com/questions/11050817/your-content-must-have-a-listview-whose-id-attribute-is-android-r-id-list – user2983227

+0

'android:id =“@ android: id/authorName“'不是'android:id =”@ android:id/list“'。所以,我没有看到你链接到的问题:[你的内容必须有一个ListView,其id属性是'android.R.id.list'](http:// stackoverflow。com/questions/11050817/your-content-must-have-a-listview-which-id-attribute-is-android-r-id-list)与* your * answer有关。 – Makyen

相关问题