我一直在创建一个应用程序,基于目标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);
答案 0 :(得分:3)
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.(JSONObject.java:154) 10-06 19:23:12.927:
E/AndroidRuntime(589): at org.json.JSONObject.(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)
它基本上说JSON解析器失败了:你可能在MainActivity.java的第156行传递NULL。您应检查该行的值,看看您的设备升级是否以某种方式消除了值或无法正确检索值。
答案 1 :(得分:1)
try {
responseBody = client.execute(get, responseHandler);
} catch (Exception ex) {
ex.printStackTrace();
}
该部分代码是您的响应失败的地方。你为响应体分配了null,很可能你的调用失败了,因此当响应第156行时,responsebody仍为null。
尝试查看ex.printStackTrace()的调试输出;输出以查看正在发生的事情。
答案 2 :(得分:0)
通过使用HttpResponse和HttpEntity而不是ResponseHandler来解决问题,这解决了这个问题。