凌空请求队列 NullPointerException

Volley Request Queue NullPointerException

我正在尝试使用 Volley 库通过用户通过 SearchWidget 提供的搜索查询在网络上查询 xml api。

每当我输入搜索字符串时,应用程序都会在将 StringRequest 添加到 RequestQueue 时崩溃并显示 NullPointerException。 为什么会这样?

我已经按照 Android Developer Guide.

中的建议使用单例模式的 Volley

这是我在 MainActivity 中的 search() 函数。

public static final String TAG = "MYAPPTAG"
private RequestQueue mRequestQueue;
...
protected void onCreate(Bundle savedInstanceState) {
    ...
    // Handle search intent
    Intent intent = getIntent();
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        search(query);
    }

    volley = MySingleton.getInstance(getApplicationContext());
    mRequestQueue = volley.getRequestQueue();
}
...
private void search(String query) {
    String xml = null;
    String url = "http://www.acme.com/example.php?query=" + query;
    StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            }
    );
    stringRequest.setTag(TAG);
    mRequestQueue
            .add(stringRequest); // the exception fires here
}
...

查看下面的堆栈跟踪。

02-22 23:08:50.636    9856-9856/com.example.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.app, PID: 9856
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
        at android.app.ActivityThread.access0(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5146)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.example.app.MainActivity.search(MainActivity.java:121)
        at com.example.app.MainActivity.onCreate(MainActivity.java:83)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
            at android.app.ActivityThread.access0(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5146)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
            at dalvik.system.NativeStart.main(Native Method)

在从单例设置请求队列之前,您正在启动搜索。

因此,在创建时,您先检查 Intent,然后开始搜索,然后再处理 volley 单例,然后再处理请求队列。尝试在 Intent 检查之前移动 volley 代码,如下所示:

volley = MySingleton.getInstance(getApplicationContext());
mRequestQueue = volley.getRequestQueue();

Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
    String query = intent.getStringExtra(SearchManager.QUERY);
    search(query);
}