排球请求队列NullPointerException

时间:2015-02-22 22:39:30

标签: java android android-volley

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

每当我输入搜索字符串时,只要NullPointerExceptionStringRequest添加到RequestQueue,应用就会崩溃。 为什么会这样?

我已经按照Android Developer Guide中的建议使用了具有单例模式的Volley。

以下是search()中的MainActivity功能。

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.access$800(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.access$800(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)

1 个答案:

答案 0 :(得分:0)

在您从单身人士设置请求队列之前,您正在开始搜索。

所以onCreate,你检查了Intent,然后开始搜索,然后你就可以处理凌空单身,然后是请求队列。尝试在Intent检查之前移动凌空代码,如下所示:

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);
}