自定义列表视图的自定义适配器。空指针异常
Custom adapter for custom listview. Null pointer exception
我的自定义适配器出现空指针异常 class。我不明白为什么。它在 holder.name.setText(s.getName()) 中给了我一个例外。请帮忙。我必须提交我的项目。另外,请解释适配器如何工作以及何时使用 ArrayAdapter、BaseAdapter 等适配器。如果我有像这种情况下的类型列表,该怎么办。什么更有利?
customadapter.java class.
public class CustomAdapter extends BaseAdapter{
private Context activity;
private LayoutInflater inflater = null;
private List<Students> student;
int layout;
public CustomAdapter(Context activity, List<Students> Student) {
this.activity = activity;
this.student = Student;
inflater = LayoutInflater.from(activity);
}
@Override
public int getCount() {
return student.size();
}
@Override
public Object getItem(int position) {
return student.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public class ViewHolder {
//ImageView imageView ;
TextView name ;
TextView usn ;
TextView semester ;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
View v =view;
ViewHolder holder = new ViewHolder();
if (view == null) {
LayoutInflater li = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(android.R.layout.simple_list_item_2,parent,false);
holder.name = (TextView)v.findViewById(R.id.name);
holder.usn = (TextView)v.findViewById(R.id.usn);
holder.semester = (TextView)v.findViewById(R.id.semester);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
Students s = new Students();
s = student.get(position);
holder.name.setText(s.getName());
holder.usn.setText(s.getUSN());
holder.semester.setText(s.getSemester());
Log.d("CustomAdapter.class", "CustomAdapter");
// imageView.setImageDrawable(s.getPic());
return v;
}
}
我的主要活动。
lv = (ListView)findViewById(R.id.listView);
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Students");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> parseObjects, ParseException e) {
if(e==null) {
for(ParseObject ob: parseObjects) {
List<Students> studentsList= new ArrayList<Students>();
s= new Students();
String a = ob.getString("Name").toString();
Log.d(TAG, a);
s.setName(a);
Log.d(TAG, s.Name);
s.setUSN(ob.getString("USN").toString());
// s.setPic(ob.getBytes());
s.setSemester(ob.getString("Semester").toString());
studentsList.add(s);
adapter = new CustomAdapter(Main2Activity.this, studentsList);
lv.setAdapter(adapter);
}
} else {
Toast.makeText(Main2Activity.this,e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
});
这是我调用适配器的片段。
list_item.xml 布局文件。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.redux.kumardivyarajat.parsedemo.Main2Activity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/name"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/usn"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/name"
android:layout_toEndOf="@+id/name"
android:layout_marginLeft="203dp"
android:layout_marginStart="203dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/semester"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
mainactivity2.xml.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.redux.kumardivyarajat.parsedemo.Main2Activity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
这是我的数据模型。
public class Students {
String Name, USN, Semester;
Drawable pic;
Students () {
};
public Students(String name, Drawable pic, String semester, String USN) {
Name = name;
this.pic = pic;
Semester = semester;
this.USN = USN;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public Drawable getPic() {
return pic;
}
public void setPic(Drawable pic) {
this.pic = pic;
}
public String getSemester() {
return Semester;
}
public void setSemester(String semester) {
Semester = semester;
}
public String getUSN() {
return USN;
}
public void setUSN(String USN) {
this.USN = USN;
}
}
您似乎正在使用 android 资源布局,因为包是 (android.R.*)。如果您想使用自己的 xml 资源,请更改行:
v = li.inflate(R.layout.your_xml_name,parent,false);
不要忘记初始化您的 ArrayList
。
改变,
private List<Students> student;
到
private List<Students> student = new List<Students>;
在 CustomAdapter.class
.
我的自定义适配器出现空指针异常 class。我不明白为什么。它在 holder.name.setText(s.getName()) 中给了我一个例外。请帮忙。我必须提交我的项目。另外,请解释适配器如何工作以及何时使用 ArrayAdapter、BaseAdapter 等适配器。如果我有像这种情况下的类型列表,该怎么办。什么更有利? customadapter.java class.
public class CustomAdapter extends BaseAdapter{
private Context activity;
private LayoutInflater inflater = null;
private List<Students> student;
int layout;
public CustomAdapter(Context activity, List<Students> Student) {
this.activity = activity;
this.student = Student;
inflater = LayoutInflater.from(activity);
}
@Override
public int getCount() {
return student.size();
}
@Override
public Object getItem(int position) {
return student.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public class ViewHolder {
//ImageView imageView ;
TextView name ;
TextView usn ;
TextView semester ;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
View v =view;
ViewHolder holder = new ViewHolder();
if (view == null) {
LayoutInflater li = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(android.R.layout.simple_list_item_2,parent,false);
holder.name = (TextView)v.findViewById(R.id.name);
holder.usn = (TextView)v.findViewById(R.id.usn);
holder.semester = (TextView)v.findViewById(R.id.semester);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
Students s = new Students();
s = student.get(position);
holder.name.setText(s.getName());
holder.usn.setText(s.getUSN());
holder.semester.setText(s.getSemester());
Log.d("CustomAdapter.class", "CustomAdapter");
// imageView.setImageDrawable(s.getPic());
return v;
}
}
我的主要活动。
lv = (ListView)findViewById(R.id.listView);
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Students");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> parseObjects, ParseException e) {
if(e==null) {
for(ParseObject ob: parseObjects) {
List<Students> studentsList= new ArrayList<Students>();
s= new Students();
String a = ob.getString("Name").toString();
Log.d(TAG, a);
s.setName(a);
Log.d(TAG, s.Name);
s.setUSN(ob.getString("USN").toString());
// s.setPic(ob.getBytes());
s.setSemester(ob.getString("Semester").toString());
studentsList.add(s);
adapter = new CustomAdapter(Main2Activity.this, studentsList);
lv.setAdapter(adapter);
}
} else {
Toast.makeText(Main2Activity.this,e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
});
这是我调用适配器的片段。 list_item.xml 布局文件。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.redux.kumardivyarajat.parsedemo.Main2Activity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/name"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/usn"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/name"
android:layout_toEndOf="@+id/name"
android:layout_marginLeft="203dp"
android:layout_marginStart="203dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/semester"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
mainactivity2.xml.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.redux.kumardivyarajat.parsedemo.Main2Activity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
这是我的数据模型。
public class Students {
String Name, USN, Semester;
Drawable pic;
Students () {
};
public Students(String name, Drawable pic, String semester, String USN) {
Name = name;
this.pic = pic;
Semester = semester;
this.USN = USN;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public Drawable getPic() {
return pic;
}
public void setPic(Drawable pic) {
this.pic = pic;
}
public String getSemester() {
return Semester;
}
public void setSemester(String semester) {
Semester = semester;
}
public String getUSN() {
return USN;
}
public void setUSN(String USN) {
this.USN = USN;
}
}
您似乎正在使用 android 资源布局,因为包是 (android.R.*)。如果您想使用自己的 xml 资源,请更改行:
v = li.inflate(R.layout.your_xml_name,parent,false);
不要忘记初始化您的 ArrayList
。
改变,
private List<Students> student;
到
private List<Students> student = new List<Students>;
在 CustomAdapter.class
.