Json数组数据未添加到ArrayList和返回空指针异常
public void select_order_info(final String selectedItem,final String month ) {
final ProgressDialog progressDialog = new ProgressDialog(getContext());
progressDialog.setTitle(R.string.app_name);
progressDialog.setMessage("Please wait while loading...");
progressDialog.setCancelable(false);
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST,
Class_Global.BASE_URL + "booking_details.php", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
//System.out.println("response="+response);
parseJSON_select_order_info(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), error.toString(), Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}) {
@Override
public Map<String, String> getParams() {
HashMap<String, String> params = new HashMap<String, String>();
params.put("selectedItem",selectedItem);
params.put("month",month);
return params;
}
};
stringRequest.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 10, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(stringRequest);
}
public void parseJSON_select_order_info(String response) {
try {
JSONArray array_booking_info = new JSONArray(response);
for (int i = 0; i < array_booking_info.length(); i++) {
JSONObject Object = array_booking_info.getJSONObject(i);
Order order=new Order();
order.setCheck_in(Object.getString("check_in"));
order.setCheck_out(Object.getString("check_out"));
order.setOrder_no(Object.getString("order_no"));
//in_data= Object.getString("check_in");
// out= Object.getString("check_out");
// order_no=Object.getString("order_no");
arrayList_order_info.add(order);
// System.out.print(array_booking_info.length());
// Toast.makeText(getContext(), arrayList_order_info.size(), Toast.LENGTH_SHORT).show();
}
arrayAdapter_calender=new ArrayAdapter_Calender(getContext(),R.layout.list_calender,arrayList,null,order_no,null,et_month,arrayList_order_info);
gridview.setAdapter(arrayAdapter_calender);
} catch (Exception e) {
Toast.makeText(getContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
}
public class ArrayAdapter_Calender extends ArrayAdapter {
Context context;
ArrayList arrayList;
ArrayList arrayList_booking;
int day_check_in;
int day_check_out;
int days;
String in;
String out;
String order_no;
String first_name,last_name;
String in_date,out_date,order_no_date;
ArrayList<Order> arrayList_order_info;
JSONArray array_booking,array_booking_info;
EditText et_month;
String selectedItem;
String month,in_data;
public ArrayAdapter_Calender(Context context, int resource, ArrayList arrayList, ArrayList arrayList_booking, String order_no, JSONArray array_booking, EditText et_month, ArrayList<Order> arrayList_order_info) {
super(context, resource, arrayList);
this.context = context;
this.arrayList = arrayList;
this.arrayList_booking=arrayList_booking;
this.day_check_in=day_check_in;
this.day_check_out=day_check_out;
this.days=days;
this.in=in;
this.out=out;
this.order_no=order_no;
this.first_name=first_name;
this.last_name=last_name;
this.array_booking=array_booking;
this.in_date=in_date;
this.out_date=out_date;
this.order_no_date=order_no_date;
this.arrayList_order_info=arrayList_order_info;
this.et_month=et_month;
this.in_data=in_data;
this.array_booking_info=array_booking_info;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View viewGuest = convertView;
LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(context.LAYOUT_INFLATER_SERVICE);
viewGuest = layoutInflater.inflate(R.layout.list_calender, null);
final TextView tv_date = (TextView) viewGuest.findViewById(R.id.tv_date);
tv_date.setText(arrayList.get(position).toString());
for(int i=1;i<=31;i++) {
if (arrayList.get(position).equals(i)) {
tv_date.setBackgroundColor(Color.YELLOW);
}
}
try {
//JSONArray array_booking = new JSONArray(response);
for (int i = 0; i < array_booking.length(); i++) {
JSONObject Object = array_booking.getJSONObject(i);
in= Object.getString("check_in");
out= Object.getString("check_out");
order_no=Object.getString("order_no");
//first_name=Object.getString("firstname");
// last_name=Object.getString("lastname");
String[] values_check_in = in.split("-");
day_check_in = Integer.parseInt(values_check_in[2]);
String[] values_check_out = out.split("-");
day_check_out = Integer.parseInt(values_check_out[2]);
//days=day_check_out-day_check_in;
// arrayList_booking.add(day_check_in);
//arrayList_booking.add(day_check_out);
// days={day_check_in,day_check_out};
// arrayList_booking.add(day_check_in);
// arrayList_booking.add(day_check_out);
for(int j=day_check_in;j<=day_check_out;j++) {
if (arrayList.get(position).equals(j)) {
tv_date.setBackgroundColor(Color.RED);
}
}
tv_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectedItem=arrayList.get(position).toString();
month=et_month.getText().toString();
// Toast.makeText(getContext(),selectedItem,Toast.LENGTH_LONG).show();
select_order_info(selectedItem,month);
try {
// JSONArray array_booking = new JSONArray(response);
for (int j = 0; j < arrayList_order_info.size(); j++) {
//Toast.makeText(getContext(),arrayList_order_info.size(), Toast.LENGTH_SHORT).show();
//JSONObject Object = array_booking_info.getJSONObject(j);
// in=arrayList_order_info.get(j).getCheck_in();
// Toast.makeText(getContext(), in, Toast.LENGTH_SHORT).show();
in= arrayList_order_info.get(j).getCheck_in();
out= arrayList_order_info.get(j).getCheck_out();
order_no=arrayList_order_info.get(j).getCheck_out();
final Dialog dialog = new Dialog(getContext());
dialog.setContentView(R.layout.dilogue_info);
dialog.setTitle("Hello");
TextView tv_check_in = (TextView) dialog.findViewById(R.id.tv_check_in);
TextView tv_check_out = (TextView) dialog.findViewById(R.id.tv_check_out);
TextView tv_order_no = (TextView) dialog.findViewById(R.id.tv_order_no);
tv_check_in.setText(in);
tv_check_out.setText(out);
tv_order_no.setText(order_no);
dialog.show();
}
}
catch (Exception e) {
Toast.makeText(getContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
catch (Exception e) {
Toast.makeText(getContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
/* gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
selectedItem=arrayList.get(i).toString();
month=et_month.getText().toString();
Toast.makeText(getContext(),selectedItem,Toast.LENGTH_LONG).show();
select_order_info(selectedItem,month);
try {
// JSONArray array_booking = new JSONArray(response);
for (int j = 0; j < array_booking_info.length(); j++) {
JSONObject Object = array_booking_info.getJSONObject(j);
in= Object.getString("check_in");
out= Object.getString("check_out");
order_no=Object.getString("order_no");
final Dialog dialog = new Dialog(getContext());
dialog.setContentView(R.layout.dilogue_info);
dialog.setTitle("Hello");
TextView tv_check_in = (TextView) dialog.findViewById(R.id.tv_check_in);
TextView tv_check_out = (TextView) dialog.findViewById(R.id.tv_check_out);
TextView tv_order_no = (TextView) dialog.findViewById(R.id.tv_order_no);
tv_check_in.setText(in);
tv_check_out.setText(out);
tv_order_no.setText(order_no);
dialog.show();
}
}
catch (Exception e) {
Toast.makeText(getContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
}
});
*/
return viewGuest;
}
}
答案 0 :(得分:1)
从parseJSON_select_order_info()
开始设置适配器
ArrayAdapter_Calender(getContext(),R.layout.list_calender,arrayList,null,order_no,null,et_month,arrayList_order_info);
gridview.setAdapter(arrayAdapter_calender);
这里你传递的是第6个param为null array_booking
中的ArrayAdapter_Calender
并且你在getView()中使用了array_booking,因此它会导致NullPointerException
for (int i = 0; i < array_booking.length(); i++) {
JSONObject Object = array_booking.getJSONObject(i);
}