我正在尝试使用自定义请求在我的应用中实现凌空,但我不断收到此错误,我无法修复。
这是错误
java.lang.NullPointerException: Attempt to invoke virtual method 'void co.app.al.app.volley.AppController.addToRequestQueue(com.android.volley.Request, java.lang.String)' on a null object reference
这是AppController.java
public class AppController extends Application {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
LruBitmapCache mLruBitmapCache;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public ImageLoader getImageLoader() {
getRequestQueue();
if (mImageLoader == null) {
getLruBitmapCache();
mImageLoader = new ImageLoader(this.mRequestQueue, mLruBitmapCache);
}
return this.mImageLoader;
}
public LruBitmapCache getLruBitmapCache() {
if (mLruBitmapCache == null)
mLruBitmapCache = new LruBitmapCache();
return this.mLruBitmapCache;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
这是自定义请求
CustomRequest jsObjRequest = new CustomRequest(Request.Method.POST,
jsonUrl,paramsLogin,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
VolleyLog.v("Response:%n %s", response.toString(4));
int code = response.getInt("code");
String message = response.getString("message");
if (code == 1) {
//Success go to verify
Toast.makeText(getActivity().getApplicationContext(),
message,
Toast.LENGTH_LONG).show();
Log.v(TAG, String.valueOf(code));
Log.v(TAG, message);
//asd
} else if (code == 2){
int newId = response.getInt("uid");
String returnDeviceToken = response.getString("device_token");
Toast.makeText(getActivity().getApplicationContext(),
message,
Toast.LENGTH_LONG).show();
Log.v(TAG, String.valueOf(newId));
//Create the new login credentials for the user and save them to the shared preferences
session.createLoginSession(newId, returnDeviceToken);
//Send the user to the home page
String mainMessage = "home";
((IndexUserActionActivity)getActivity()).startMainActivity(mainMessage);
}else{
Toast.makeText(getActivity().getApplicationContext(),
"Network Error",
Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: ", error.getMessage());
}
});
//requestQueue.add(jsObjRequest);
AppController.getInstance().addToRequestQueue(jsObjRequest, tag_json_obj);
奇怪的是,这个完全相同的代码在我正在制作的另一个应用程序上正常工作,所以我看不出什么是错误的
感谢您的帮助
答案 0 :(得分:19)
检查您的Manifest.xml,并将以下行添加到应用程序标记下的Manifest文件中。
android:name="com.example.logintest.app.AppController"
不要忘记更改包名称(com.example.logintest)