0

我是 Android 开发和 SO 的新手,几个月前才开始。到目前为止,你们帮了大忙!我正在使用尽可能多的外部库。

我正在使用VolleyJSONObject向服务器发出 GET 请求。问题是

W/dalvikvm:threadid=1:线程以未捕获的异常退出(组=0x40a6c9d8)

被抛出,我在尝试使用响应中的对象时得到 NPE。

这在我使用 JSONObjectRequest 的其他时候不会发生,只是这个。我研究并发现了有关 AndroidManifest.xml 中应用程序标记中的 android:name 字段的信息。我不认为是这样。

我怀疑这是关于在不同的线程中提出请求,但为什么其他时间不会发生......?

好吧,我的 logcat 和代码如下。请帮我!!!

日志猫:

07-16 02:01:37.466  22635-22635/com.revmob.android.setup W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40a6c9d8)
07-16 02:01:37.476  22635-22635/com.revmob.android.setup E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.revmob.android.setup/com.pubmobile.ellow.Controller.Main.MatchFragment.MatchRequestActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1975)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2000)
        at android.app.ActivityThread.access$600(ActivityThread.java:123)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4443)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.pubmobile.ellow.Controller.Main.MatchFragment.MatchRequestActivity.onCreate(MatchRequestActivity.java:109)
        at android.app.Activity.performCreate(Activity.java:4668)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1939)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2000)
        at android.app.ActivityThread.access$600(ActivityThread.java:123)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4443)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
        at dalvik.system.NativeStart.main(Native Method)

我的代码: MatchRequestActivity.java

String url = Constants.URL_SERVER;
JsonObjectRequest getMatchRequest = new JsonObjectRequest(com.android.volley.Request.Method.GET, url, new com.android.volley.Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
        if(response != null) {

            System.out.println(response);

        } else{
            System.out.println("null Response from getMatchRequest");
        }
    }
}, new com.android.volley.Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        System.out.println("no Response from getMatchRequest");
    }
});
RequestQueueManager.getInstance(this).addToRequestQueue(getMatchRequest);

我现在拥有的:

虽然我的服务器发送响应很好,但我的代码甚至没有输入onResponse()并引发错误。然后NPE由于缺乏响应。

4

2 回答 2

0

当我将您的代码复制并粘贴到 Android Studio 中时,它会显示一条错误消息(见下文。您的代码是底部请求。)我对其进行了重新设计并添加了您缺少的参数,并且它不再抛出错误。如果这是你的错误,我不肯定。您在 MatchRequestActivity.java 的第 109 行收到空指针异常,但尚未披露该行的内容。检查一下,看看它是否有效。

        JsonObjectRequest getMatchRequest = new JsonObjectRequest(Request.Method.GET,
            url, null, new com.android.volley.Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            if(response != null) {

                System.out.println(response);

            } else{
                System.out.println("null Response from getMatchRequest");
            }
        }
    }, new com.android.volley.Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            System.out.println("no Response from getMatchRequest");
        }
    });

比较请求的屏幕截图

于 2015-07-16T15:55:44.047 回答
0

我通过一个简单的修复解决了我的问题:我必须在我的请求中添加标题。

更具体地说,带有字符串“”的“接受编码”标头。

下面的代码:

JsonObjectRequest getMatchRequest = new JsonObjectRequest(Request.Method.GET,
        url, null, new com.android.volley.Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
        if(response != null) {

            System.out.println(response);

        } else{
            System.out.println("null Response from getMatchRequest");
        }
    }
}, new com.android.volley.Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        System.out.println("no Response from getMatchRequest");
    }
}) {
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("accept-encoding", "");
        return headers;
    }
};
于 2015-09-08T17:28:24.770 回答