Android 如何在listview中使用Baseadapter?为什么会出现空点异常?
我试图使用baseadapter将元素插入listview。我想将菜单插入listview。我使用适配器DatasetAdapter将行膨胀到listview中,但此行出现空指针异常-Android 如何在listview中使用Baseadapter?为什么会出现空点异常?,android,android-listview,baseadapter,Android,Android Listview,Baseadapter,我试图使用baseadapter将元素插入listview。我想将菜单插入listview。我使用适配器DatasetAdapter将行膨胀到listview中,但此行出现空指针异常-row=inflater.inflate(R.layout.menu_detail,parent,false) DataSetAdapter.java public class DatasetAdapter extends BaseAdapter { private Context context; priv
row=inflater.inflate(R.layout.menu_detail,parent,false)代码>
DataSetAdapter.java
public class DatasetAdapter extends BaseAdapter {
private Context context;
private String[] menuitems;
LayoutInflater inflater;
public DatasetAdapter(Context cont, String[] menu) {
this.context=cont;
this.menuitems=menu;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return menuitems.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
if (row == null)
row = inflater.inflate(R.layout.menu_detail, parent,false);
TextView titlerow = (TextView) row.findViewById(R.id.detail);
return row;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:gravity="center"
android:background="#5ba4e5"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="8pt"
android:textColor="#000000"
android:layout_gravity="center"
android:id="@+id/detail"/>
</LinearLayout>
菜单\u detail.java
public class DatasetAdapter extends BaseAdapter {
private Context context;
private String[] menuitems;
LayoutInflater inflater;
public DatasetAdapter(Context cont, String[] menu) {
this.context=cont;
this.menuitems=menu;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return menuitems.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
if (row == null)
row = inflater.inflate(R.layout.menu_detail, parent,false);
TextView titlerow = (TextView) row.findViewById(R.id.detail);
return row;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:gravity="center"
android:background="#5ba4e5"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="8pt"
android:textColor="#000000"
android:layout_gravity="center"
android:id="@+id/detail"/>
</LinearLayout>
您将获得NullPointerException
,因为调用getCount
时menuitems
为空。改变
DatasetAdapter adapter=new DatasetAdapter(this,menu);
dList.setAdapter(adapter);
menu = new String[]{"Feedback","Help","Tour","Sign in/Sign up"};
到
您的getItem
应该返回位置处的项目。您现在不使用它,但将来可能会使用它:
@Override
public Object getItem(int position) {
return menuitems[position];
}
在构造函数中,您从不初始化充气机
public DatasetAdapter(Context cont, String[] menu) {
this.context=cont;
this.menuitems=menu;
inflater = LayoutInflater.from(cont);
}
您可以避免保留对上下文的引用,因为您没有在任何地方使用它尝试添加此行LayoutInflater inflater=((活动)上下文)代码>在此之前行=充气机。充气(R.layout.menu\u详细信息,父项,false)代码>我得到的菜单项长度为4(打印时)。getitem有问题吗?我编辑了我的答案,解决了您代码中的一些问题…我得到的是listview,但其中没有文本。让我检查一下。