抛出以下异常:
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:100)
at android.widget.ListView.setAdapter(ListView.java:491)
at android.app.ListActivity.setListAdapter(ListActivity.java:265)
at com.example.example.feed.ActivityFeedActivity$GetStudents.onPostExecute(ActivityFeedActivity.java:93)
at com.example.example.feed.ActivityFeedActivity$GetStudents.onPostExecute(ActivityFeedActivity.java:47)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
代码:
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class ActivityFeedActivity extends ListActivity {
// URL to get contacts JSON
private static String url = "https://raw.githubusercontent.com/mobilesiri/JSON-Parsing-in-Android/master/index.html";
// JSON Node names
private static final String TAG_STUDENTINFO = "studentsinfo";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_ADDRESS = "address";
private static final String TAG_GENDER = "gender";
private static final String TAG_PHONE = "phone";
private static final String TAG_PHONE_MOBILE = "mobile";
private static final String TAG_PHONE_HOME = "home";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activityfeed);
// Calling async task to get json
new GetStudents().execute();
}
/**
* Async task class to get json by making HTTP call
*/
private class GetStudents extends AsyncTask<Void, Void, Void> {
// Hashmap for ListView
ArrayList<HashMap<String, String>> studentList = new ArrayList<HashMap<String, String>>();;
ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(ActivityFeedActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
WebRequest webreq = new WebRequest();
// Making a request to url and getting response
String jsonStr = webreq.makeWebServiceCall(url, WebRequest.GET);
Log.d("Response: ", "> " + jsonStr);
studentList = ParseJSON(jsonStr);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
ActivityFeedActivity.this, studentList,
R.layout.feed_item, new String[]{TAG_NAME, TAG_EMAIL,
TAG_PHONE_MOBILE}, new int[]{R.id.name,
R.id.email, R.id.mobile});
setListAdapter(adapter);
}
}
private ArrayList<HashMap<String, String>> ParseJSON(String json) {
if (json != null) {
try {
// Hashmap for ListView
ArrayList<HashMap<String, String>> studentList = new ArrayList<HashMap<String, String>>();
JSONObject jsonObj = new JSONObject(json);
// Getting JSON Array node
JSONArray students = jsonObj.getJSONArray(TAG_STUDENTINFO);
// looping through All Students
for (int i = 0; i < students.length(); i++) {
JSONObject c = students.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
String address = c.getString(TAG_ADDRESS);
String gender = c.getString(TAG_GENDER);
// Phone node is JSON Object
JSONObject phone = c.getJSONObject(TAG_PHONE);
String mobile = phone.getString(TAG_PHONE_MOBILE);
String home = phone.getString(TAG_PHONE_HOME);
// tmp hashmap for single student
HashMap<String, String> student = new HashMap<String, String>();
// adding each child node to HashMap key => value
student.put(TAG_ID, id);
student.put(TAG_NAME, name);
student.put(TAG_EMAIL, email);
student.put(TAG_PHONE_MOBILE, mobile);
// adding student to students list
studentList.add(student);
}
return studentList;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
return null;
}
}
}
如果我理解正确的话,当OnPostExecute()方法执行时,应该从url解析studentList中的数据,那么我错过了什么导致异常抛出呢?
答案 0 :(得分:0)
问题可能在于您的JSON解析实现,因为如果引发 <html lang="en" >
<head>
<meta charset="utf-8">
<!--<script type="text/javascript" src="included_name_table.js"></script> -->
<script type="text/javascript" src="apps.js"></script>
<script src="name_template.js"></script>
<scr
<center><h1>JavaScript Variables</h1></center>
<script type="text/javascript" src="vra_config.js"></script>
<center><p>In this example, x, y, and z are variables</p></center>
<!--<center><p id="demo"></p></center> -->
<!--<script type="text/javascript" src="demo"></script> -->
<body onload="initialize('name');">
<script>
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = port_list_var;
document.body.appendChild(script);
</script>
<p id="port_list_var"></p>
<!--<script type="text/javascript" src="check.js"></script> -->
<p> <center> Check for Table </center> </p>
<table height="100px" width="840px" align="center" cellspacing=0 cellpadding=0 border=2>
<tr><td colspan="3"><span id="name_table_body"></span></td></tr>
<br>
</table>
<center> <button style="width:180px;margin-top:5px;margin-bottom:20px;" onClick="submitForm();">Submit Results</button>
</body>
</html>
则返回null
或实际请求,因为在JSON不是的情况下您也返回JSONException
成功获取。在崩溃之前LogCat中是否还有另一个例外?