我正在尝试制作ListView。但是,发生错误,
“尝试在空对象引用上调用虚方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'”。
这是BookedCourseListAdpater的代码
public class BookedCourseListAdapter extends BaseAdapter{
private Context context;
private List<BookedCourse> BookedList;
private Activity parentActivity;
public BookedCourseListAdapter(Context context, List<BookedCourse> BookedCourseList, Activity parentActivity){
this.context = context;
this.BookedList = BookedCourseList;
this.parentActivity = parentActivity;
}
@Override
public int getCount() {
return BookedList.size();
}
@Override
public Object getItem(int position) {
return BookedList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
@SuppressLint("ViewHolder") View v = View.inflate(context, result, null);
//View v = View.inflate(context,R.layout.result, null);
final TextView BookedCourseTeacher = (TextView) v.findViewById(R.id.BookedCourseTeacher);
final TextView BookedCourseDay = (TextView) v.findViewById(R.id.BookedCourseDay);
final TextView BookedCourseTime = (TextView) v.findViewById(R.id.BookedCourseTime);
BookedCourseTeacher.setText(BookedList.get(position).getBookedCourseTeacher());
BookedCourseDay.setText(BookedList.get(position).getBookedCourseDay());
BookedCourseTime.setText(BookedList.get(position).getBookedCourseTime());
v.setTag(BookedList.get(position).getBookedCourseID());
Button cancelButton = (Button) v.findViewById(R.id.cancelButton);
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Response.Listener<String> responseListener = new Response.Listener<String>(){
public void onResponse(String response){
try{
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if(success){
BookedList.remove(position);
notifyDataSetChanged();
}
}catch(Exception e){
e.printStackTrace();
}
}
};
CancelRequest cancelRequest = new CancelRequest(BookedList.get(position).getBookedCourseID() +" ",BookedList.get(position).getBookedCourseBranch().toString(), responseListener);
RequestQueue queue = Volley.newRequestQueue(parentActivity);
queue.add(cancelRequest);
}
});
return v;
}
}
这是BookedCourse的代码
public class BookedCourse {
String BookedCourseTeacher;
String BookedCourseDay;
String BookedCourseTime;
int BookedCourseID;
String BookingUserID;
String BookedCourseBranch;
public String getBookedCourseBranch() {
return BookedCourseBranch;
}
public void setBookedCourseBranch(String bookedCourseBranch) {
BookedCourseBranch = bookedCourseBranch;
}
public String getBookingUserID() {
return BookingUserID;
}
public void setBookingUserID(String bookingUserID) {
BookingUserID = bookingUserID;
}
public int getBookedCourseID() {
return BookedCourseID;
}
public void setBookedCourseID(int bookedCourseID) {
BookedCourseID = bookedCourseID;
}
public BookedCourse(String bookingUserID, String bookedCourseTeacher, String bookedCourseDay, String bookedCourseTime, int bookedCourseID, String bookedCourseBranch) {
this.BookingUserID = bookingUserID;
this.BookedCourseTeacher = bookedCourseTeacher;
this.BookedCourseDay = bookedCourseDay;
this.BookedCourseTime = bookedCourseTime;
this.BookedCourseID = bookedCourseID;
this.BookedCourseBranch = bookedCourseBranch;
}
public String getBookedCourseTeacher() {
return BookedCourseTeacher;
}
public void setBookedCourseTeacher(String bookedCourseTeacher) {
BookedCourseTeacher = bookedCourseTeacher;
}
public String getBookedCourseDay() {
return BookedCourseDay;
}
public void setBookedCourseDay(String bookedCourseDay) {
BookedCourseDay = bookedCourseDay;
}
public String getBookedCourseTime() {
return BookedCourseTime;
}
public void setBookedCourseTime(String bookedCourseTime) {
BookedCourseTime = bookedCourseTime;
}
}
以下是从MySQL解析数据的部分
private int parseData(){
try {
JSONArray ja = new JSONArray(jsonData);
JSONObject jo = null;
BookedList.clear();
BookedCourse bookedCourse = null;
for(int i = 0; i <ja.length();i++)
{
jo = ja.getJSONObject(i);
String bookinguserID = jo.getString("userID");
int id = jo.getInt("courseID");
String courseBranch = jo.getString("courseBranch");
String bookedCourseTeacher = jo.getString("courseTeacher");
String bookedCourseDay = jo.getString("courseDay");
String bookedCourseTime = jo.getString("courseTime");
if(bookinguserID.equals(userID))
{
bookedCourse = new BookedCourse(bookinguserID, bookedCourseTeacher, bookedCourseDay, bookedCourseTime, id, courseBranch);
BookedList.add(bookedCourse);
}
}
}
return 1;
}catch (JSONException e){
e.printStackTrace();
}
return 0;
}
这是setAdapter到listview的部分
public class Result extends AppCompatActivity {
private ListView resultlist;
private BookedCourseListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
resultlist = (ListView) findViewById(R.id.userListView);
adapter = new BookedCourseListAdapter(getApplicationContext(),BookedList, this);
resultlist.setAdapter(adapter);
}
}
我不认为BookedList为null。那为什么在空对象上调用setAdapter?
答案 0 :(得分:0)
我在任何地方都看不到parseData()
的来电。
这是我的猜测,但是您可能没有将任何对象分配到列表中吗?
最好将此处的整个活动发布在此处。