在onResponse排球库中使用接口时,我得到一个空指针异常

时间:2014-12-22 15:25:48

标签: java android interface android-volley

当我尝试使用凌空库时,我得到一个空指针异常。在我的代码下面,你能帮帮我吗。(对不起我的英文):

问题是他无法实例化委托类。

Singleton类MenuWebService:

公共类MenuWebService {

public static final String TAG = MenuWebService.class.getSimpleName();
private String tag_json_arry = "tag_json_arry";
private static MenuWebService mInstance = null;

private ArrayList<Menu> listMenu = new ArrayList<Menu>();
private RequestCallback delegate;
private Menu menuObject;

public static MenuWebService getInstance() {
    if (mInstance == null) {
        mInstance = new MenuWebService();
    }
    return mInstance;
}

public void getMenu() {

    JsonArrayRequest req = new JsonArrayRequest(Const.URL_JSON_MENU,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    JSONObject menu;
                    Log.d(TAG,"Response :"+ response.toString());
                    for (int i = 0; i < response.length(); i++) {

                        try {
                            menu = (JSONObject) response.get(i);
                            menuObject = new Menu(menu);
                            listMenu.add(menuObject);

                        } catch (JSONException e) {

                            e.printStackTrace();
                        }
                    }
                    delegate.onGetMenuSuccess(listMenu);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    if (error != null) {

                        delegate.onError(error.getMessage());

                    }
                }
            });

    AppController.getInstance().addToRequestQueue(req, tag_json_arry);

}

}

RequestCallback.java:

public interface RequestCallback {

public void onGetMenuSuccess(ArrayList<Menu> listMenu);

public void onError(String message);

}

这是日志错误:

12-22 16:22:29.626: E/AndroidRuntime(15910): FATAL EXCEPTION: main
12-22 16:22:29.626: E/AndroidRuntime(15910): Process: com.mobvalue.livefoot.fr, PID: 15910
12-22 16:22:29.626: E/AndroidRuntime(15910): java.lang.NullPointerException
12-22 16:22:29.626: E/AndroidRuntime(15910):    at com.mobvalue.livefoot.fr.services.MenuWebService$1.onResponse(MenuWebService.java:61)
12-22 16:22:29.626: E/AndroidRuntime(15910):    at com.mobvalue.livefoot.fr.services.MenuWebService$1.onResponse(MenuWebService.java:1)
12-22 16:22:29.626: E/AndroidRuntime(15910):    at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
12-22 16:22:29.626: E/AndroidRuntime(15910):    at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
12-22 16:22:29.626: E/AndroidRuntime(15910):    at android.os.Handler.handleCallback(Handler.java:733)
12-22 16:22:29.626: E/AndroidRuntime(15910):    at android.os.Handler.dispatchMessage(Handler.java:95)
12-22 16:22:29.626: E/AndroidRuntime(15910):    at android.os.Looper.loop(Looper.java:157)
12-22 16:22:29.626: E/AndroidRuntime(15910):    at android.app.ActivityThread.main(ActivityThread.java:5356)
12-22 16:22:29.626: E/AndroidRuntime(15910):    at java.lang.reflect.Method.invokeNative(Native Method)
12-22 16:22:29.626: E/AndroidRuntime(15910):    at java.lang.reflect.Method.invoke(Method.java:515)
12-22 16:22:29.626: E/AndroidRuntime(15910):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
12-22 16:22:29.626: E/AndroidRuntime(15910):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
12-22 16:22:29.626: E/AndroidRuntime(15910):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您尚未初始化请求回拨代理,并且您正在使用它:

delegate.onGetMenuSuccess(listMenu);

如果您初始化它,那将解决您的问题。