[英]Custom adapter for custom listview. Null pointer exception
I am getting a Null Pointer exception in my custom adapter class. 我的自定义适配器类中出现了空指针异常。 I dont understand why. 我不明白为什么。 Its giving me an exception in holder.name.setText(s.getName()). 它给我holder.name.setText(s.getName())中的异常。 Please help. 请帮忙。 I have to submit my project. 我必须提交我的项目。 Also, please explain how adapters work and when to use which adapter such as ArrayAdapter, BaseAdapter. 另外,请说明适配器如何工作以及何时使用哪种适配器,例如ArrayAdapter,BaseAdapter。 What to do if I have a list of my types like in this case. 如果我有这种情况下的类型列表,该怎么办。 What is more beneficial? 有什么更有益的? The customadapter.java class. customadapter.java类。
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;
}
}
My main acitivity . 我的主要爱好。
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();
}
}
});
This is the snippet where I call the adapter. 这是我称为适配器的代码段。 The list_item.xml layout file. 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>
The mainactivity2.xml. 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>
And this is my data model. 这是我的数据模型。
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;
}
}
It seems you are using an android resource layout, because the package is (android.R.*). 似乎您使用的是android资源布局,因为该软件包是(android.R。*)。 If you want using your own xml resource, change the line: 如果要使用自己的xml资源,请更改以下行:
v = li.inflate(R.layout.your_xml_name,parent,false);
Don't forget to Initialize your ArrayList
. 不要忘记初始化ArrayList
。
change, 更改,
private List<Students> student;
to 至
private List<Students> student = new List<Students>;
in CustomAdapter.class
. 在CustomAdapter.class
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.