我正在尝试通过JSON将我的Android Studio App连接到远程数据库。当我启动我的应用程序时,它崩溃了
Error Logcat是这样的:
10-09 11:27:48.166 19973-20495 / com.example.alessandro.celiachia E / AndroidRuntime:致命异常:AsyncTask#2 10-09 11:27:48.166 19973-20495 / com.example.alessandro.celiachia E / AndroidRuntime: 过程:com.example.alessandro.celiachia,PID:19973 10-09 11:27:48.166 19973-20495 / com.example.alessandro.celiachia E / AndroidRuntime:java.lang.RuntimeException:发生错误 执行doInBackground()10-09 11:27:48.166 19973-20495 / com.example.alessandro.celiachia E / AndroidRuntime:at android.os.AsyncTask $ 3.done(AsyncTask.java:300)10-09 11:27:48.166 19973-20495 / com.example.alessandro.celiachia E / AndroidRuntime:at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 10-09 11:27:48.166 19973-20495 / com.example.alessandro.celiachia E / AndroidRuntime:at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 10-09 11:27:48.166 19973-20495 / com.example.alessandro.celiachia E / AndroidRuntime:at java.util.concurrent.FutureTask.run(FutureTask.java:242)10-09 11:27:48.166 19973-20495 / com.example.alessandro.celiachia E / AndroidRuntime:at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)10-09 11:27:48.166 19973-20495 / com.example.alessandro.celiachia E / AndroidRuntime:at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 10-09 11:27:48.166 19973-20495 / com.example.alessandro.celiachia E / AndroidRuntime:at java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587) 10-09 11:27:48.166 19973-20495 / com.example.alessandro.celiachia E / AndroidRuntime:在java.lang.Thread.run(Thread.java:818)10-09 11:27:48.166 19973-20495 / com.example.alessandro.celiachia E / AndroidRuntime:引起:java.lang.NullPointerException:Attempt 调用虚方法'org.json.JSONArray null对象上的org.json.JSONObject.getJSONArray(java.lang.String)' 参考10-09 11:27:48.166 19973-20495 / com.example.alessandro.celiachia E / AndroidRuntime:at com.example.alessandro.celiachia.MarkerJSONParser.parse(MarkerJSONParser.java:23) 10-09 11:27:48.166 19973-20495 / com.example.alessandro.celiachia E / AndroidRuntime:at com.example.alessandro.celiachia.MapActivity $ ParserTask.doInBackground(MapActivity.java:174) 10-09 11:27:48.166 19973-20495 / com.example.alessandro.celiachia E / AndroidRuntime:at com.example.alessandro.celiachia.MapActivity $ ParserTask.doInBackground(MapActivity.java:164) 10-09 11:27:48.166 19973-20495 / com.example.alessandro.celiachia E / AndroidRuntime:在android.os.AsyncTask $ 2.call(AsyncTask.java:288) 10-09 11:27:48.166 19973-20495 / com.example.alessandro.celiachia E / AndroidRuntime:at java.util.concurrent.FutureTask.run(FutureTask.java:237)10-09 11:27:48.166 19973-20495 / com.example.alessandro.celiachia E / AndroidRuntime:at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)10-09 11:27:48.166 19973-20495 / com.example.alessandro.celiachia E / AndroidRuntime:at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 10-09 11:27:48.166 19973-20495 / com.example.alessandro.celiachia E / AndroidRuntime:at java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587) 10-09 11:27:48.166 19973-20495 / com.example.alessandro.celiachia E / AndroidRuntime:在java.lang.Thread.run(Thread.java:818)
我使用的代码是:
public class MarkerJSONParser {
static JSONArray jMarkers = null;
/** Receives a JSONObject and returns a list */
public List<HashMap<String,String>> parse(JSONObject jObject){
try {
/** Retrieves all the elements in the 'markers' array */
jMarkers = jObject.getJSONArray("marker");
} catch (JSONException e) {
e.printStackTrace();
}
/** Invoking getMarkers with the array of json object
* where each json object represent a marker
*/
return getMarkers(jMarkers);
}
private List<HashMap<String, String>> getMarkers(JSONArray jMarkers){
int markersCount = jMarkers.length();
List<HashMap<String, String>> markersList = new ArrayList<HashMap<String,String>>();
HashMap<String, String> marker = null;
/** Taking each marker, parses and adds to list object */
for(int i=0; i<markersCount;i++){
try {
/** Call getMarker with marker JSON object to parse the marker */
marker = getMarker((JSONObject)jMarkers.get(i));
markersList.add(marker);
}catch (JSONException e){
e.printStackTrace();
}
}
return markersList;
}
/** Parsing the Marker JSON object */
private HashMap<String, String> getMarker(JSONObject jMarker){
HashMap<String, String> marker = new HashMap<String, String>();
String lat = "-NA-";
String lng ="-NA-";
try {
// Extracting latitude, if available
if(!jMarker.isNull("lat")){
lat = jMarker.getString("lat");
}
// Extracting longitude, if available
if(!jMarker.isNull("lng")){
lng = jMarker.getString("lng");
}
marker.put("lat", lat);
marker.put("lng", lng);
} catch (JSONException e) {
e.printStackTrace();
}
return marker;
}
}
我希望有人可以帮助我,我不明白错误。非常感谢