2012-12-18 51 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

相關問題