我一直在创建一个应用程序,构建在目标 sdk 版本 15 和最低 sdk 版本 8 上。当我在运行版本 8 的设备上运行它时,每件事都运行完美。但是当我在高于版本 10 的任何设备上尝试它时,它会崩溃带有 NullPointerException。
logcat 给了我这个:
10-06 19:23:12.927: E/AndroidRuntime(589): FATAL EXCEPTION: main
10-06 19:23:12.927: E/AndroidRuntime(589): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fansheroid.facts.chicks/com.fansheroid.facts.chicks.MainActivity}: java.lang.NullPointerException
10-06 19:23:12.927: E/AndroidRuntime(589): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
10-06 19:23:12.927: E/AndroidRuntime(589): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
10-06 19:23:12.927: E/AndroidRuntime(589): at android.app.ActivityThread.access$600(ActivityThread.java:122)
10-06 19:23:12.927: E/AndroidRuntime(589): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
10-06 19:23:12.927: E/AndroidRuntime(589): at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 19:23:12.927: E/AndroidRuntime(589): at android.os.Looper.loop(Looper.java:137)
10-06 19:23:12.927: E/AndroidRuntime(589): at android.app.ActivityThread.main(ActivityThread.java:4340)
10-06 19:23:12.927: E/AndroidRuntime(589): at java.lang.reflect.Method.invokeNative(Native Method)
10-06 19:23:12.927: E/AndroidRuntime(589): at java.lang.reflect.Method.invoke(Method.java:511)
10-06 19:23:12.927: E/AndroidRuntime(589): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-06 19:23:12.927: E/AndroidRuntime(589): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-06 19:23:12.927: E/AndroidRuntime(589): at dalvik.system.NativeStart.main(Native Method)
10-06 19:23:12.927: E/AndroidRuntime(589): Caused by: java.lang.NullPointerException
10-06 19:23:12.927: E/AndroidRuntime(589): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
10-06 19:23:12.927: E/AndroidRuntime(589): at org.json.JSONTokener.nextValue(JSONTokener.java:94)
10-06 19:23:12.927: E/AndroidRuntime(589): at org.json.JSONObject.<init>(JSONObject.java:154)
10-06 19:23:12.927: E/AndroidRuntime(589): at org.json.JSONObject.<init>(JSONObject.java:171)
10-06 19:23:12.927: E/AndroidRuntime(589): at com.fansheroid.facts.chicks.MainActivity.getTumblrs(MainActivity.java:156)
10-06 19:23:12.927: E/AndroidRuntime(589): at com.fansheroid.facts.chicks.MainActivity.onCreate(MainActivity.java:62)
10-06 19:23:12.927: E/AndroidRuntime(589): at android.app.Activity.performCreate(Activity.java:4465)
10-06 19:23:12.927: E/AndroidRuntime(589): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-06 19:23:12.927: E/AndroidRuntime(589): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
10-06 19:23:12.927: E/AndroidRuntime(589): ... 11 more
在过去的两天里,我一直试图找出问题所在,但这对我来说没有意义。
编辑添加的代码
public ArrayList<Tumblr> getTumblrs() throws ClientProtocolException,
IOException, JSONException {
String searchUrl = "http://api.tumblr.com/v2/blog/factsandchicks.com/posts?api_key=API_KEY";
ArrayList<Tumblr> tumblrs = new ArrayList<Tumblr>();
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(searchUrl);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = null;
try {
responseBody = client.execute(get, responseHandler);
} catch (Exception ex) {
ex.printStackTrace();
}
JSONObject jsonObject = new JSONObject(responseBody);
JSONArray posts = jsonObject.getJSONObject("response").getJSONArray(
"posts");
for (int i = 0; i < posts.length(); i++) {
JSONArray photos = posts.getJSONObject(i).getJSONArray("photos");
for (int j = 0; j < photos.length(); j++) {
JSONObject photo = photos.getJSONObject(j);
String url = photo.getJSONArray("alt_sizes").getJSONObject(0)
.getString("url");
Tumblr tumblr = new Tumblr(url);
tumblrs.add(tumblr);
}
}
return tumblrs;
}
第 156 行:
JSONObject jsonObject = new JSONObject(responseBody);