我知道有类似的问题,但即使我看了这些问题,我也无法让它发挥作用。错误信息是:
java.lang.RuntimeException:您的内容必须有一个 id 属性为 'android.R.id.list' 的 ListView
. 我试图将 id 更改为列表。在另一个。这是代码的样子:
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;
}
}
这是指定列表(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="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>
这是列表项的 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>
我希望你能找出问题所在。我在项目中的另一个 xml 文件中有另一个具有相同 ID(列表)的列表视图。我不知道这是否有问题。请帮我解决问题。