我正在使用普通循环从文件中提取json信息,并将其放入普通的数组适配器中,一切都已正确设置,但运行该应用程序时我总是崩溃,我修改了所有代码,但没有错误。这是代码:
如果需要,我可以添加其他代码。
private QueryUtils() {
}
/**
* Return a list of {@link Earthquake} objects that has been built up from
* parsing a JSON response.
*/
public static ArrayList<Earthquake> extractEarthquakes() {
// Create an empty ArrayList that we can start adding earthquakes to
ArrayList<Earthquake> earthquakes = new ArrayList<>();
// Try to parse the SAMPLE_JSON_RESPONSE. If there's a problem with the way the JSON
// is formatted, a JSONException exception object will be thrown.
// Catch the exception so the app doesn't crash, and print the error message to the logs.
try {
// Create a JSONObject from the SAMPLE_JSON_RESPONSE string
JSONObject baseJsonResponse = new JSONObject(SAMPLE_JSON_RESPONSE);
// Extract the JSONArray associated with the key called "features",
// which represents a list of features (or earthquakes).
JSONArray earthquakeArray = baseJsonResponse.getJSONArray("features");
// For each earthquake in the earthquakeArray, create an {@link Earthquake} object
for (int i = 0; i < earthquakeArray.length(); i++) {
// Get a single earthquake at position i within the list of earthquakes
JSONObject currentEarthquake = earthquakeArray.getJSONObject(i);
// For a given earthquake, extract the JSONObject associated with the
// key called "properties", which represents a list of all properties
// for that earthquake.
JSONObject properties = currentEarthquake.getJSONObject("properties");
// Extract the value for the key called "mag"
// Extract the value for the key called "place"
String location = properties.getString("place");
String magnitude = properties.getString("mag");
// Extract the value for the key called "time"
String time = properties.getString("time");
// Extract the value for the key called "url"
// Create a new {@link Earthquake} object with the magnitude, location, time,
// and url from the JSON response.
Earthquake earthquake = new Earthquake(magnitude, location, time);
// Add the new {@link Earthquake} to the list of earthquakes.
earthquakes.add(earthquake);
}
} catch (JSONException e) {
// If an error is thrown when executing any of the above statements in the "try" block,
// catch the exception here, so the app doesn't crash. Print a log message
// with the message from the exception.
Log.e("QueryUtils", "Problem parsing the earthquake JSON results", e);
}
// Return the list of earthquakes
return earthquakes;
}
}
这是错误消息:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.quakereport, PID: 8512
android.content.res.Resources$NotFoundException: Resource ID
#0x0
the logcat image the error image
显然,该错误不是json解析错误,它是从getview方法中找不到的资源,因此我以图像形式添加了此代码,因为我无法识别它们: the first part of the code the secound part of the code
答案 0 :(得分:0)
删除return super.getView(position,convertView,parent);
是错的,如果convertView为null,则不需要返回/调用getView()
,只需要对其进行膨胀即可。我建议您查找有关自定义ArrayAdapter
的教程。