java.lang.IllegalArgumentException、java.lang.NullPointerException、org.json.JSONException 在 android 项目中
java.lang.IllegalArgumentException, java.lang.NullPointerException, org.json.JSONException in android project
package com.example.attendance;
import java.util.ArrayList;
import java.util.Calendar;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class StudentListActivity extends Activity {
MyCustomAdapter dataAdapter = null;
JSONObject jsonobject;
JSONArray jsonarray;
ArrayList<String> studentlist;
ArrayList<Student> student;
String route_id;
String school_id;
String trip;
String travel_mode;
String name;
String id;
String returnval;
String status;
int total;
int present;
int absent;
Calendar c = Calendar.getInstance();
int day = c.get(Calendar.DATE);
int month = c.get(Calendar.MONTH);
int year = c.get(Calendar.YEAR);
String todate = day + "/" + (month + 1) + "/" + year;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_list);
SharedPreferences editor = getSharedPreferences("school", MODE_PRIVATE);
route_id = editor.getString("routeid", "");
school_id = editor.getString("schoolid", "");
trip = editor.getString("trip", "");
travel_mode = editor.getString("travelmode", "");
//Generate list View from ArrayList
//displayListView();
new DownloadJSON().execute();
checkButtonClick();
}
//Download JSON file AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
// Locate the WorldPopulation Class
student = new ArrayList<Student>();
// Create an array to populate the spinner
studentlist = new ArrayList<String>();
// JSON file URL address
if (travel_mode == "pickup") {
jsonobject = JSONFunctions
.getJSONfromURL("http://attendance- samsidh.rhcloud.com/getstudentsforattendancepickup?tripid=" + trip + "&schol=" + school_id + "&routeid=" + route_id);
} else {
jsonobject = JSONFunctions
.getJSONfromURL("http://attendance-samsidh.rhcloud.com/getstudentsforattendancedrop?tripid=" + trip + "&schol=" + school_id + "&routeid=" + route_id);
}
try {
// Locate the NodeList name
jsonarray = jsonobject.getJSONArray("returnval");
//System.out.println(jsonarray);
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
Student s = new Student();
s.setStudentid(jsonobject.optString("student_id"));
s.setStudentname(jsonobject.optString("name"));
//s.setSelected(jsonobject.optBoolean(name, fallback)("flag"));
student.add(s);
// Populate spinner with country names
// studentlist.add(jsonobject.optString("name"));
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
displayListView();
}
}
private void displayListView() {
//create an ArrayAdaptar from the String Array
dataAdapter = new MyCustomAdapter(this, R.layout.student_info_list, student);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Student student = (Student) parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(), "Clicked on Row: " + student.getStudentname(),
Toast.LENGTH_LONG).show();
}
});
}
private class MyCustomAdapter extends ArrayAdapter<Student> {
private ArrayList<Student> studentList;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<Student> studentList) {
super(context, textViewResourceId, studentList);
this.studentList = new ArrayList<Student>();
this.studentList.addAll(studentList);
}
private class ViewHolder {
TextView code;
CheckBox name;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
// Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.student_info_list, null);
holder = new ViewHolder();
holder.code = (TextView) convertView.findViewById(R.id.code);
holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
holder.name.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Student student = (Student) cb.getTag();
Toast.makeText(getApplicationContext(), "Clicked on Checkbox: " + cb.getText() + " is " + cb.isChecked(), Toast.LENGTH_LONG).show();
student.setSelected(cb.isChecked());
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
Student student = studentList.get(position);
holder.code.setText(" (" + student.getStudentid() + ")");
holder.name.setText(student.getStudentname());
holder.name.setChecked(student.isSelected());
holder.name.setTag(student);
return convertView;
}
}
private void checkButtonClick() {
Button myButton = (Button) findViewById(R.id.findSelected);
myButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
total = 0;
present = 0;
absent = 0;
StringBuffer responseText = new StringBuffer();
responseText.append("The following were selected...\n");
ArrayList<Student> studentList = dataAdapter.studentList;
for (int i = 0; i < studentList.size(); i++) {
total = total + 1;
Student student = studentList.get(i);
if (student.isSelected()) {
present = present + 1;
status = String.valueOf(1);
//responseText.append("\n" + student.getStudentname());
/* name=student.getStudentname();
id=student.getStudentid();
jsonobject = JSONFunctions
.getJSONfromURL("http://attendance-samsidh.rhcloud.com/attsubmiturl?schol=SCH001&studentid="+ id +"&student_name="+ name +"&routeid="+ route_id +"&pickupordrop="+ travel_mode +"&trip="+ trip +"&date="+ todate +"&status=1");*/
//responseText.append("\n" + student.getStudentname());
name = student.getStudentname();
// name="praba";
id = student.getStudentid();
new ExecuteTask().execute(id, name);
// jsonobject = JSONFunctions.getJSONfromURL("http://attendance-samsidh.rhcloud.com/attsubmiturl?schol="+school_id+"&studentid="+ id +"&student_name="+ name +"&routeid="+ route_id +"&pickupordrop="+ travel_mode +"&trip="+ trip +"&date="+ todate +"&status="+ status +"");
// jsonobject = JSONFunctions.getJSONfromURL("http://attendance-samsidh.rhcloud.com/attsubmiturl?schol=SCH001&studentid=ENR11125&student_name=ram&routeid=ROUTE-01&pickupordrop=pickup&trip=1&date=25/07/2016&status=1");
} else {
absent = absent + 1;
status = String.valueOf(0);
name = student.getStudentname();
id = student.getStudentid();
// jsonobject = JSONFunctions.getJSONfromURL("http://attendance-samsidh.rhcloud.com/attsubmiturl?schol="+school_id+"&studentid="+ id +"&student_name="+ name +"&routeid="+ route_id +"&pickupordrop="+ travel_mode +"&trip="+ trip +"&date="+ todate +"&status="+ status +"");
//jsonobject = JSONFunctions.getJSONfromURL("http://attendance-samsidh.rhcloud.com/attsubmiturl?schol=SCH001&studentid="+ id +"&student_name="+ name +"&routeid="+ route_id +"&pickupordrop="+ travel_mode +"&trip="+ trip +"&date="+ todate +"&status=0");
}
}
/*Bundle b=new Bundle();
b.putInt("Total",total); //create a key and store data in bundle
b.putInt("Present",present);
b.putInt("Absent",absent);
Toast.makeText(getApplicationContext(),"Absent: " + absent +" present :" + present, Toast.LENGTH_LONG).show();
Intent i=new Intent(StudentListActivity.this,AttendanceDetail.class);
i.putExtras(b);
startActivity(i);
*/
}
});
}
class ExecuteTask extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
String res = PostData(params);
//jsonobject = JSONFunctions.getJSONfromURL("http://attendance-samsidh.rhcloud.com/attsubmiturl?schol=SCH001&studentid=ENR11125&student_name=ram&routeid=ROUTE-01&pickupordrop=pickup&trip=1&date=25/07/2016&status=1");
jsonobject = JSONFunctions.getJSONfromURL("http://attendance-samsidh.rhcloud.com/attsubmiturl?schol=" + school_id + "&studentid=" + id + "&student_name=" + name + "&routeid=" + route_id + "&pickupordrop=" + travel_mode + "&trip=" + trip + "&date=" + todate + "&status=1");
try {
returnval = jsonobject.getString("returnval");
jsonarray = jsonobject.getJSONArray("returnval");
returnval = jsonarray.get(0).toString();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), returnval, 3000).show();
return returnval;
}
private String PostData(String[] params) {
// TODO Auto-generated method stub
return null;
}
@Override
protected void onPostExecute(String result) {
// progressBar.setVisibility(View.GONE);
//progess_msz.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), result, 3000).show();
}
}
}
上面的代码是如果从列表中选中复选框以在主机中执行 Web 服务,则调用 AsyncTask 或 ExecuteTask 函数。
绑定到查询或 Web 服务的静态值工作正常,但在将值与变量绑定时出现这些错误。
java.lang.IllegalArgumentException,
java.lang.NullPointerException,
org.json.JSONException
请给我一个解决问题的建议或想法
您的 JSON 似乎没有您要获取的密钥。
如果你能 Post 年 JSON 你试图解析它以便 C..
package com.example.attendance;
import java.util.ArrayList;
import java.util.Calendar;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class StudentListActivity extends Activity {
MyCustomAdapter dataAdapter = null;
JSONObject jsonobject;
JSONArray jsonarray;
ArrayList<String> studentlist;
ArrayList<Student> student;
String route_id;
String school_id;
String trip;
String travel_mode;
String name;
String id;
String returnval;
String status;
int total;
int present;
int absent;
Calendar c = Calendar.getInstance();
int day = c.get(Calendar.DATE);
int month = c.get(Calendar.MONTH);
int year = c.get(Calendar.YEAR);
String todate = day + "/" + (month + 1) + "/" + year;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_list);
SharedPreferences editor = getSharedPreferences("school", MODE_PRIVATE);
route_id = editor.getString("routeid", "");
school_id = editor.getString("schoolid", "");
trip = editor.getString("trip", "");
travel_mode = editor.getString("travelmode", "");
//Generate list View from ArrayList
//displayListView();
new DownloadJSON().execute();
checkButtonClick();
}
//Download JSON file AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
// Locate the WorldPopulation Class
student = new ArrayList<Student>();
// Create an array to populate the spinner
studentlist = new ArrayList<String>();
// JSON file URL address
if (travel_mode == "pickup") {
jsonobject = JSONFunctions
.getJSONfromURL("http://attendance- samsidh.rhcloud.com/getstudentsforattendancepickup?tripid=" + trip + "&schol=" + school_id + "&routeid=" + route_id);
} else {
jsonobject = JSONFunctions
.getJSONfromURL("http://attendance-samsidh.rhcloud.com/getstudentsforattendancedrop?tripid=" + trip + "&schol=" + school_id + "&routeid=" + route_id);
}
try {
// Locate the NodeList name
jsonarray = jsonobject.getJSONArray("returnval");
//System.out.println(jsonarray);
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
Student s = new Student();
s.setStudentid(jsonobject.optString("student_id"));
s.setStudentname(jsonobject.optString("name"));
//s.setSelected(jsonobject.optBoolean(name, fallback)("flag"));
student.add(s);
// Populate spinner with country names
// studentlist.add(jsonobject.optString("name"));
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
displayListView();
}
}
private void displayListView() {
//create an ArrayAdaptar from the String Array
dataAdapter = new MyCustomAdapter(this, R.layout.student_info_list, student);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Student student = (Student) parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(), "Clicked on Row: " + student.getStudentname(),
Toast.LENGTH_LONG).show();
}
});
}
private class MyCustomAdapter extends ArrayAdapter<Student> {
private ArrayList<Student> studentList;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<Student> studentList) {
super(context, textViewResourceId, studentList);
this.studentList = new ArrayList<Student>();
this.studentList.addAll(studentList);
}
private class ViewHolder {
TextView code;
CheckBox name;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
// Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.student_info_list, null);
holder = new ViewHolder();
holder.code = (TextView) convertView.findViewById(R.id.code);
holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
holder.name.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Student student = (Student) cb.getTag();
Toast.makeText(getApplicationContext(), "Clicked on Checkbox: " + cb.getText() + " is " + cb.isChecked(), Toast.LENGTH_LONG).show();
student.setSelected(cb.isChecked());
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
Student student = studentList.get(position);
holder.code.setText(" (" + student.getStudentid() + ")");
holder.name.setText(student.getStudentname());
holder.name.setChecked(student.isSelected());
holder.name.setTag(student);
return convertView;
}
}
private void checkButtonClick() {
Button myButton = (Button) findViewById(R.id.findSelected);
myButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
total = 0;
present = 0;
absent = 0;
StringBuffer responseText = new StringBuffer();
responseText.append("The following were selected...\n");
ArrayList<Student> studentList = dataAdapter.studentList;
for (int i = 0; i < studentList.size(); i++) {
total = total + 1;
Student student = studentList.get(i);
if (student.isSelected()) {
present = present + 1;
status = String.valueOf(1);
//responseText.append("\n" + student.getStudentname());
/* name=student.getStudentname();
id=student.getStudentid();
jsonobject = JSONFunctions
.getJSONfromURL("http://attendance-samsidh.rhcloud.com/attsubmiturl?schol=SCH001&studentid="+ id +"&student_name="+ name +"&routeid="+ route_id +"&pickupordrop="+ travel_mode +"&trip="+ trip +"&date="+ todate +"&status=1");*/
//responseText.append("\n" + student.getStudentname());
name = student.getStudentname();
// name="praba";
id = student.getStudentid();
new ExecuteTask().execute(id, name);
// jsonobject = JSONFunctions.getJSONfromURL("http://attendance-samsidh.rhcloud.com/attsubmiturl?schol="+school_id+"&studentid="+ id +"&student_name="+ name +"&routeid="+ route_id +"&pickupordrop="+ travel_mode +"&trip="+ trip +"&date="+ todate +"&status="+ status +"");
// jsonobject = JSONFunctions.getJSONfromURL("http://attendance-samsidh.rhcloud.com/attsubmiturl?schol=SCH001&studentid=ENR11125&student_name=ram&routeid=ROUTE-01&pickupordrop=pickup&trip=1&date=25/07/2016&status=1");
} else {
absent = absent + 1;
status = String.valueOf(0);
name = student.getStudentname();
id = student.getStudentid();
// jsonobject = JSONFunctions.getJSONfromURL("http://attendance-samsidh.rhcloud.com/attsubmiturl?schol="+school_id+"&studentid="+ id +"&student_name="+ name +"&routeid="+ route_id +"&pickupordrop="+ travel_mode +"&trip="+ trip +"&date="+ todate +"&status="+ status +"");
//jsonobject = JSONFunctions.getJSONfromURL("http://attendance-samsidh.rhcloud.com/attsubmiturl?schol=SCH001&studentid="+ id +"&student_name="+ name +"&routeid="+ route_id +"&pickupordrop="+ travel_mode +"&trip="+ trip +"&date="+ todate +"&status=0");
}
}
/*Bundle b=new Bundle();
b.putInt("Total",total); //create a key and store data in bundle
b.putInt("Present",present);
b.putInt("Absent",absent);
Toast.makeText(getApplicationContext(),"Absent: " + absent +" present :" + present, Toast.LENGTH_LONG).show();
Intent i=new Intent(StudentListActivity.this,AttendanceDetail.class);
i.putExtras(b);
startActivity(i);
*/
}
});
}
class ExecuteTask extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
String res = PostData(params);
//jsonobject = JSONFunctions.getJSONfromURL("http://attendance-samsidh.rhcloud.com/attsubmiturl?schol=SCH001&studentid=ENR11125&student_name=ram&routeid=ROUTE-01&pickupordrop=pickup&trip=1&date=25/07/2016&status=1");
jsonobject = JSONFunctions.getJSONfromURL("http://attendance-samsidh.rhcloud.com/attsubmiturl?schol=" + school_id + "&studentid=" + id + "&student_name=" + name + "&routeid=" + route_id + "&pickupordrop=" + travel_mode + "&trip=" + trip + "&date=" + todate + "&status=1");
try {
returnval = jsonobject.getString("returnval");
jsonarray = jsonobject.getJSONArray("returnval");
returnval = jsonarray.get(0).toString();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), returnval, 3000).show();
return returnval;
}
private String PostData(String[] params) {
// TODO Auto-generated method stub
return null;
}
@Override
protected void onPostExecute(String result) {
// progressBar.setVisibility(View.GONE);
//progess_msz.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), result, 3000).show();
}
}
}
上面的代码是如果从列表中选中复选框以在主机中执行 Web 服务,则调用 AsyncTask 或 ExecuteTask 函数。
绑定到查询或 Web 服务的静态值工作正常,但在将值与变量绑定时出现这些错误。
java.lang.IllegalArgumentException,
java.lang.NullPointerException,
org.json.JSONException
请给我一个解决问题的建议或想法
您的 JSON 似乎没有您要获取的密钥。 如果你能 Post 年 JSON 你试图解析它以便 C..