1

我一直在创建一个应用程序,构建在目标 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);
4

3 回答 3

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。您应该检查该行的值,看看您的设备升级是否没有以某种方式清除值或未能正确检索值。

于 2012-10-06T19:38:52.557 回答
1
try {
    responseBody = client.execute(get, responseHandler);
} catch (Exception ex) {
    ex.printStackTrace();
}

这部分代码是您的响应失败的地方。您将 null 分配给 responsebody,您的调用可能会失败,因此 responsebody 在到达第 156 行时仍然为 null。

尝试在您的调试输出中查找 ex.printStackTrace(); 输出看看发生了什么。

于 2012-10-06T19:52:56.867 回答
0

通过使用 HttpResponse 和 HttpEntity 而不是 ResponseHandler 解决了这个问题,这解决了这个问题。

于 2012-10-06T22:23:54.497 回答