2013-07-28 18 views
8

Tôi biết rằng đã có câu hỏi simialr nhưng tôi không thể làm cho nó hoạt động ngay cả khi tôi nhìn vào . Các thông báo lỗi là:java.lang.RuntimeException: Nội dung của bạn phải có một ListView có thuộc tính id là 'android.R.id.list'

java.lang.RuntimeException: Nội dung của bạn phải có một ListView có id thuộc tính là 'android.R.id.list'

. Tôi đã cố gắng thay đổi id thành danh sách. Trong cái khác. Đây gì mã trông giống như:

public class committeeFragment extends SherlockListFragment { 

    private CommitteeAdapter adapter; 

    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     adapter = new CommitteeAdapter(getActivity().getApplicationContext()); 
     setListAdapter(adapter); 
    } 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     return inflater.inflate(R.layout.fragment_committee, container, false); 
    } 

    public void 
    refreshList() { 
     adapter.updateDataSet(); 
     adapter.notifyDataSetChanged(); 
    } 
} 

Đó là sử dụng bộ chuyển đổi sau:

public class CommitteeAdapter extends BaseAdapter { 
    private 
    ArrayList<StudentCommittee> committeeList; 
    private Context context; 

    public CommitteeAdapter(Context context) { 
     this.context = context; 
     CommitteeDatabaseAdapter dbAdapter = new CommitteeDatabaseAdapter(context); 
     committeeList = dbAdapter.readAllCommittees(); 
    } 

    public void updateDataSet() { 
     CommitteeDatabaseAdapter dbAdapter = new CommitteeDatabaseAdapter(context); 
     committeeList = dbAdapter.readAllCommittees(); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     StudentCommittee committee = committeeList.get(position); 

     View v = vi.inflate(R.layout.list_item_committee, null); 
     TextView sectionName = (TextView) v.findViewById(R.id.committee_namn); 

     sectionName.setText(committee.getName()); 
     return v; 
    } 

    @Override 
    public int getCount() { 
     return committeeList.size(); 
    } 

    @Override 
    public StudentCommittee getItem(int position) { 
     return committeeList.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 
} 

Dưới đây là xml-file đó quy định cụ thể danh sách (fragment_committee):

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:tag="@string/tag_fragment_committee" 
    android:background="@color/white" > 

    <TextView 
     style="@style/AppsolutText.Headline" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Festerier" /> 

     <View 
     android:layout_width="fill_parent" 
     android:layout_height="2dp" 
     android:background="@drawable/divider_sliding_menu_item"/> 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <TextView 
     style="@style/AppsolutText.Normal" 
     android:id="@android:id/empty" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="Uppdatera för att se en lista över festerier och fadderier. Kräver internetanslutning."/> 

</LinearLayout> 

Đây là xml cho mục danh sách:

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

    <ImageView 
     android:id="@+id/committee_symbol" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:paddingBottom="10dp" 
     android:paddingRight="10dp" 
     android:paddingTop="10dp" /> 

    <TextView 
     android:id="@+id/committee_namn" 
     style="@style/AppsolutText.ListHeader" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 

</LinearLayout> 

Tôi hy vọng rằng bạn có thể tìm ra điều gì sai. Tôi có một listview khác có cùng một id (danh sách) trong một xml-file khác trong dự án. Tôi không biết đó có phải là vấn đề không. Xin hãy giúp tôi giải quyết vấn đề.

+0

bản sao có thể có của [ListView bên trong phân đoạn cung cấp lỗi] (http://stackoverflow.com/questions/12294819/listview-inside-a-fragment-gives-error) – Raghunandan

+0

đăng lớp R.java –

Trả lời

23

Vấn đề là ở đây:

android:id="@+id/list" 

bạn đang thêm một id mới cho ListView của bạn nhưng ListFragment cần một mặc định Android ListView Id

thay đổi ListView của bạn Id để:

<ListView 
    android:id="@id/android:list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 
+0

@Arash nếu chúng tôi muốn triển khai trên trình nghe nhấp chuột với listview này, sau đó làm thế nào chúng ta có thể đạt được điều đó? – AndroidOptimist

+0

@Alliswell: bạn phải ghi đè void công khai onListItemClick (ListView cha mẹ, Xem v, vị trí int, id dài) \t {}. hy vọng nó sẽ giúp –

+0

@Arash tôi muốn mở listview con từ mỗi hàng mà không cần sử dụng lớp bộ điều hợp. U có thể cho tôi một ví dụ cho – AndroidOptimist

5

Thay đổi ListView trong XML của bạn để theo dõi g id:

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

Đó là một yêu cầu của việc sử dụng ListFragment/SherlockListFragment

http://developer.android.com/reference/android/app/ListFragment.html

ListFragment đã bố trí mặc định mà bao gồm một giao diện danh sách duy nhất. Tuy nhiên, nếu bạn muốn, bạn có thể tùy chỉnh bố cục phân đoạn bằng cách trả về hệ thống phân cấp chế độ xem của riêng bạn từ onCreateView(LayoutInflater, ViewGroup, Bundle). Để thực hiện việc này, phân cấp chế độ xem của bạn phải chứa một đối tượng ListView có id "@android: id/list" (hoặc list nếu nó ở trong mã)

+0

không cần sử dụng android cho id. android: id = "@ + id/listIdName". nó cũng chính xác –

+0

Nếu bạn sử dụng 'ListFragment' bạn làm, nếu không bạn sẽ nhận được lỗi. –

0

Bạn có thể thử làm sạch dự án của mình. Nếu có bất kỳ lỗi xml releted nào, bạn có thể tìm thấy nó. Sau đó, bạn có thể thay đổi id danh sách của mình. Tìm id danh sách tồn tại trong lớp R.java thư mục gen của bạn.

0

Tôi quyết định ngừng sử dụng bộ đệm hành động và sử dụng thư viện hỗ trợ thay vào đó để nhận thanh tác vụ trong Android 2.1 trở lên. Sau khi làm điều này, tôi đã nhận được "java.lang.RuntimeException: Nội dung của bạn phải có một ListView có thuộc tính id là 'android.R.id.list'".Những gì tôi đã làm để khắc phục vấn đề này đã được

để đọc http://developer.android.com/reference/android/app/ListFragment.html#q=fragment

sau đó thay đổi file layout mảnh của tôi từ

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/item_detail" 
    style="?android:attr/textAppearanceLarge" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="16dp" 
    android:textIsSelectable="true" 
    tools:context=".ItemDetailFragment" /> 

để

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="8dp" 
    android:paddingRight="8dp" 
    tools:context=".ItemDetailFragment"> 

<ListView android:id="@id/android:list" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#00FF00" 
      android:layout_weight="1" 
      android:drawSelectorOnTop="false"/> 

<TextView android:id="@+id/item_detail" 
     style="?android:attr/textAppearanceLarge" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:padding="16dp" 
     android:textIsSelectable="true"/> 

<TextView android:id="@id/android:empty" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#FF0000" 
      android:text="No data"/> 

... và thì đấy, RunTimeException với android.R.id.list bị mất đã biến mất! Tất nhiên, tôi cần chỉnh sửa bố cục mới của mình và sửa nó đúng cách, nhưng vấn đề chính ở đây là tôi đã ghi đè bố cục đoạn trong phương thức onCreateView của nó. ActionBarSherlock DID KHÔNG có vấn đề với điều này, nhưng với thư viện hỗ trợ Android, nếu bạn làm điều này, bạn PHẢI có một ListView trong Giao diện XML của bạn, với android: id = "@ id/android: list" như đã nêu trong thông tin liên kết ở trên.

Hy vọng điều này sẽ giúp (đến từ tổng số newb trong dev ứng dụng Android)!

+0

Tôi muốn thêm vào thông tin này vì tôi đã sử dụng ListFragment, tôi KHÔNG có thẻ ListView trong XML ở bất kỳ đâu, cho ListFragment này. ListView được tích hợp vào ListFragment nhưng có thể bị ghi đè. Vì vậy tôi không thể chỉ thay đổi ListView android: id như nhiều người đề xuất, bởi vì tôi không thể tìm thấy bất kỳ thẻ ListView nào trong XML liên quan đến ListFragment của tôi. Do đó các giải pháp trên - để thêm một! – TiredDude

+0

Một bình luận khác cho bài viết của tôi ở trên: Tôi là một tổng số newb cho điều này và nghĩ rằng tôi đã sử dụng một ListFragment khi tôi không muốn có một danh sách ở nơi đầu tiên. Làm cho cảm giác rằng một ListFragment cần ít nhất một ListView. Tôi sẽ sử dụng một loại Fragment để tôi không phải thực hiện một ListView không sử dụng trong XML của tôi chỉ để thoát khỏi RunTimeException. Gaah! – TiredDude

Các vấn đề liên quan