我想创建一个自定义列表视图,但是当我运行程序时它会因异常而中断:
日志猫:
在onCreate方法中:
ListView list = (ListView) findViewById(android.R.id.list);
String items[] = {"cat", "dog", "horse", "monkey"};
list.setAdapter(new myAdapter(items));
在myAdapter类中:
private class myAdapter extends ArrayAdapter<String>{
private String items[];
public myAdapter(String items[]) {
super(ListViewExample.this, R.layout.row, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.row, parent);
TextView label=(TextView)view.findViewById(R.id.row_textView1);
label.setText(items[position]);
return view;
}
}
我的主要活动:
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
我的模板行:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/row_textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
我该如何解决这个问题?谢谢