Android 片段中的listView出错
我正在尝试在ListFragment中创建ListView。我的列表是自定义的,所以它是用适配器创建的。但是应用程序会出错,并告诉我这个错误您的内容必须有一个id属性为“android.r.id.list”的listview 这是ListFragment类:Android 片段中的listView出错,android,listview,fragment,Android,Listview,Fragment,我正在尝试在ListFragment中创建ListView。我的列表是自定义的,所以它是用适配器创建的。但是应用程序会出错,并告诉我这个错误您的内容必须有一个id属性为“android.r.id.list”的listview 这是ListFragment类: public static class DummySectionFragment extends ListFragment { public View onCreateView(LayoutInflater inflater, V
public static class DummySectionFragment extends ListFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_dummy, container, false);
Vector<String> names=new Vector<String>();
names.add("Alex");
names.add("Julia");
setListAdapter(new MyAdapter(getActivity(), names));
return rootView;
}
}
这是MyAdapter类:
public class MiAdaptador extends BaseAdapter {
private final Activity activ;
private final Vector<String> list;
public MiAdaptador(Activity activ, Vector<String> list) {
super();
this.actividad = activ;
this.list = list;
}
public View getView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = activ.getLayoutInflater();
View view = inflater.inflate(R.layout.adapter_view, null, true);
TextView textView =(TextView)view.findViewById(R.id.titulo);
textView.setText(lista.elementAt(position));
return view;
}
}
我的碎片\u部分\u虚拟布局:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name list"
android:gravity="center"
android:layout_margin="10px"
android:textSize="10pt"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false" />
<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
和我的适配器视图布局:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight">
<TextView android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:singleLine="true"
android:text="title"
android:gravity="center"/>
<TextView android:id="@+id/subtitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Other text"
android:layout_below="@id/title"
android:layout_alignParentBottom="true"
android:gravity="center"/>
</RelativeLayout>
我该如何解决这个问题?我开始认为您应该将充气视图附加到根视图 将通货膨胀率改为: 视图根视图=充气机。充气机。布局。碎片\截面\虚拟,容器,真 另一种选择可能是仅使用onCreateView方法对主布局进行充气。并使用onStart方法设置列表适配器。这样可以确保Android知道您的自定义布局
发布完整的stacktrace有助于解决您的问题。还有其他类像DummySectionFragment一样扩展到ListFrament,但这些类不会膨胀具有ListView的布局。因此,我必须更改该类片段的父级,而不是ListFragment,它已经解决了。
谢谢你的回答顺便说一句,我建议你使用ArrayList而不是Vector,Vector已经过时了:好的,谢谢,我会更改它!