java.lang.RuntimeException:您的内容必须具有id属性为';android.R.id.list';

java.lang.RuntimeException:您的内容必须具有id属性为';android.R.id.list';,android,listview,Android,Listview,我知道也有类似的问题,但我不能让它工作,即使我看这些。错误消息是: java.lang.RuntimeException:您的内容必须具有id为的ListView 属性为“android.R.id.list” 。我已尝试将id更改为list。另一方面。这是代码的外观: public class committeeFragment extends SherlockListFragment { private CommitteeAdapter adapter; public v

我知道也有类似的问题,但我不能让它工作,即使我看这些。错误消息是:

java.lang.RuntimeException:您的内容必须具有id为的ListView 属性为“android.R.id.list”

。我已尝试将id更改为list。另一方面。这是代码的外观:

 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();
    }
}
它正在使用以下适配器:

    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;
    }
}
公共类CommitteeAdapter扩展BaseAdapter{
私有的
ArrayList委员会成员;
私人语境;
公共委员会接受者(上下文){
this.context=上下文;
CommitteeDatabaseAdapter dbAdapter=新CommitteeDatabaseAdapter(上下文);
committeeList=dbAdapter.readAllCommittees();
}
public void updateDataSet(){
CommitteeDatabaseAdapter dbAdapter=新CommitteeDatabaseAdapter(上下文);
committeeList=dbAdapter.readAllCommittees();
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutInflater vi=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
学生委员会=委员会列表get(职位);
视图v=vi.充气(R.布局.列表项目委员会,空);
TextView sectionName=(TextView)v.findViewById(R.id.committee\u nam);
sectionName.setText(committee.getName());
返回v;
}
@凌驾
public int getCount(){
返回committeeList.size();
}
@凌驾
公立学生委员会项目(国际职位){
返回委员会列表。获取(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
}
以下是指定列表的xml文件(fragment_committee):


以下是列表项的xml:

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

我希望你能找出问题所在。我有另一个listview,它在项目中的另一个xml文件中具有相同的id(列表)。我不知道这是否是个问题。请帮我解决问题。

问题在这里:

android:id="@+id/list"
您正在为ListView添加一个新id,但ListFragment需要一个默认的Android ListView id

将ListView Id更改为:



将XML中的
列表视图更改为具有以下id:

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

这是使用
ListFragment
/
SherlockListFragment

ListFragment具有由单个列表视图组成的默认布局。但是,如果需要,可以通过从
onCreateView(LayoutFlater、ViewGroup、Bundle)
返回自己的视图层次结构来定制片段布局。为此,视图层次结构必须包含id为“@android:id/list”的ListView对象(或
list
,如果它在代码中)


你可以尝试清理你的项目。如果有任何与xml相关的错误,您可以找到它。然后您可以更改您的列表id。查找存在于gen文件夹R.java类中的列表id。

我决定停止使用actionbarsherlock,而是使用支持库来获取Android 2.1及更高版本中的操作栏。这样做之后,我得到了“java.lang.RuntimeException:您的内容必须有一个id属性为'android.R.id.list'的ListView”。我为解决这个问题所做的是

读这个

然后将我的片段布局文件从

<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" />


。。。瞧,缺少android.R.id.list的RunTimeException消失了!当然,我需要编辑我的新布局并正确地修复它,但这里的关键问题是我已经在其onCreateView方法中重写了片段布局。ActionBarSherlock对此没有问题,但对于Android支持库,如果您这样做,您的XML布局中必须有一个列表视图,其中的Android:id=“@id/Android:list”如上面链接的信息中所述


希望这有帮助(来自Android应用开发的新手)

您的R.java类的帖子可能重复,不需要使用android进行id。android:id=“@+id/listIdName”。如果您使用
ListFragment
也可以,否则会出现错误。我想在此信息中补充一点,因为我使用的是ListFragment,所以对于此ListFragment,我没有XML格式的ListView标记。ListView内置于ListFragment中,但可以被覆盖。因此,我不能像许多人建议的那样仅仅更改ListView android:id,因为我在XML中找不到任何与我的ListFragment相关的ListView标记。因此,上述解决方案-添加一个!上面我自己的帖子的另一个评论是:我对这一点完全是新手,我认为我在一开始不想要列表的时候使用了ListFragment。ListFragment至少需要一个ListView是有道理的。我将使用另一种类型的片段,这样我就不必在XML中实现一个未使用的ListView来摆脱RunTimeException。嘎@Arash如果我们想用这个listview实现点击监听器,那么我们如何实现呢?@Alliswell:您必须重写public void onListItemClick(listview父项,视图v,int位置,长id){}。希望如此helps@Arash我想在不使用适配器类的情况下从每一行打开子listview。你能给我举个例子吗this@Alliswell:请参阅ExpandableListView,但我对没有适配器类的您的遗嘱没有任何线索@Arash在片段中使用适配器可以实现同样的效果。。这里我需要子视图位于下一个布局上,而不是位于同一个布局上。。
<ListView 
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
<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"/>