Java 自定义截击请求上的NullPointerException
我正在尝试使用自定义请求在我的应用程序中实现截击,但我一直遇到无法修复的错误 这里是错误Java 自定义截击请求上的NullPointerException,java,android,nullpointerexception,android-volley,Java,Android,Nullpointerexception,Android Volley,我正在尝试使用自定义请求在我的应用程序中实现截击,但我一直遇到无法修复的错误 这里是错误 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 下面是App
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);
}
}
}
公共类AppController扩展应用程序{
公共静态最终字符串标记=AppController.class.getSimpleName();
私有请求队列mRequestQueue;
私有图像加载器;
LruBitmapCache-mLruBitmapCache;
专用静态应用控制器;
@凌驾
public void onCreate(){
super.onCreate();
mInstance=这个;
}
公共静态同步AppController getInstance(){
回报率;
}
公共请求队列getRequestQueue(){
if(mRequestQueue==null){
mRequestQueue=Volley.newRequestQueue(getApplicationContext());
}
返回mrequest队列;
}
公共ImageLoader getImageLoader(){
getRequestQueue();
if(mImageLoader==null){
getLruBitmapCache();
mImageLoader=新的ImageLoader(this.mRequestQueue,mLruBitmapCache);
}
返回此.mImageLoader;
}
公共LruBitmapCache getLruBitmapCache(){
if(mLruBitmapCache==null)
mLruBitmapCache=新的LruBitmapCache();
返回此.mLruBitmapCache;
}
公共无效addToRequestQueue(请求请求,字符串标记){
请求setTag(TextUtils.isEmpty(tag)?tag:tag;
getRequestQueue().add(请求);
}
公共无效addToRequestQueue(请求请求){
要求设置标签(标签);
getRequestQueue().add(请求);
}
公共作废取消挂起请求(对象标记){
if(mRequestQueue!=null){
mRequestQueue.cancelAll(标记);
}
}
}
这是定制请求
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);
CustomRequest jsObjRequest=新的CustomRequest(Request.Method.POST,
jsonUrl,帕拉斯洛金,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
VolleyLog.v(“响应:%n%s”,响应.toString(4));
int code=response.getInt(“代码”);
字符串消息=response.getString(“消息”);
如果(代码==1){
//成功去验证
Toast.makeText(getActivity().getApplicationContext(),
消息
Toast.LENGTH_LONG).show();
Log.v(TAG,String.valueOf(code));
Log.v(标签、消息);
//自闭症
}否则如果(代码==2){
int newId=response.getInt(“uid”);
String returnDeviceToken=response.getString(“设备令牌”);
Toast.makeText(getActivity().getApplicationContext(),
消息
Toast.LENGTH_LONG).show();
Log.v(TAG,String.valueOf(newId));
//为用户创建新的登录凭据并将其保存到共享首选项
创建登录会话(newId,returnDeviceToken);
//将用户发送到主页
字符串mainMessage=“home”;
((IndexUserActionActivity)getActivity()).startMainActivity(主消息);
}否则{
Toast.makeText(getActivity().getApplicationContext(),
“网络错误”,
Toast.LENGTH_LONG).show();
}
}捕获(JSONException e){
e、 printStackTrace();
}
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
e(“Error:,Error.getMessage());
}
});
//add(jsObjRequest);
AppController.getInstance().addToRequestQueue(jsObjRequest,tag_json_obj);
奇怪的是,这个完全相同的代码在我正在制作的另一个应用程序上运行良好,所以我看不出哪里出了问题
感谢您的帮助检查Manifest.xml,并将下面的行添加到应用程序标记下的清单文件中
android:name="com.example.logintest.app.AppController"
不要忘记更改包名(com.example.logintest)您的应用程序实例看起来是空的。您是否在清单中指定了自定义应用程序?我怀疑你的
AppController
没有被Android使用。请发布您的声明,请添加更多日志。一行可能不正确enough@PavelDudka你完全正确,当我复制代码时,我完全忘记了清单声明。谢谢你的帮助检查这个你能解释我为什么要添加这行吗@曼迪,如果你不能,因为你有多重索引?