java - 如何在不崩溃的情况下提取 JSON 信息?
问题描述
我正在使用普通循环从文件中提取 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
显然错误不是json解析错误它是从getview方法中找不到的资源所以我以图像的形式添加了这个代码,因为我无法识别它们: 代码的第一部分代码 的第二部分
解决方案
删除return super.getView(position,convertView,parent);
你做错了,getView()
如果convertView为null,你不需要返回/调用,你只需要膨胀它。我建议您查找有关 custom 的教程ArrayAdapter
。
推荐阅读
- peoplesoft - 将 winmessage 用于方法外的变量时出错
- asp.net-core - 是否可以使用 Sustainsys.Saml2 在 Startup.cs 之外更改 SAML IdentityProvider 选项?
- java - 使用 GWT.Create 和 UI-Binder 创建实例的流程
- r - 案例编号是否等于 Lime 中的数据行编号?
- web3 - web3.eth.accounts.create 和 web3.eth.personal.newAccount 都不起作用
- javascript - 使用 AngularJS 进行验证
- arrays - 在 VBA 中声明数组变量
- java - FragmentActivity 与 Fragment 类型的 GoogleMap 交互
- certbot - Certbot-auto renew failed - 客户端缺乏足够的授权 - 为什么?
- reactjs - 使用 webpack 构建 React 应用程序返回错误“您可能需要适当的加载程序来处理此文件类型”