Android getApplicationContext()和#x27;在非活动中使用截击的空对象引用

Android getApplicationContext()和#x27;在非活动中使用截击的空对象引用,android,json,android-volley,Android,Json,Android Volley,花点时间尝试从android调用json,但问题是上下文混乱,这是我返回的错误 02-09 19:48:05.494 16350-16350/com.example.cesar.mybankaccess E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.cesar.mybankaccess, PID: 16350 java.lang.NullPointerException: Attempt to invoke vi

花点时间尝试从android调用json,但问题是上下文混乱,这是我返回的错误

    02-09 19:48:05.494 16350-16350/com.example.cesar.mybankaccess E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cesar.mybankaccess, PID: 16350
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at restful.dao.PatronSingleton.getRequestQueue(PatronSingleton.java:37)
at restful.dao.PatronSingleton.<init>(PatronSingleton.java:25)
at restful.dao.PatronSingleton.getInstance(PatronSingleton.java:30)
at restful.dao.Cliente.dologin(Cliente.java:84)
at restful.RestfulOperacional.dologin(RestfulOperacional.java:22)
at com.example.cesar.mybankaccess.view.Login.dologin(Login.java:118)
at com.example.cesar.mybankaccess.view.Login.access$100(Login.java:30)
at com.example.cesar.mybankaccess.view.Login$2.onClick(Login.java:67)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
并尝试解析JSON

public class Cliente extends Application {
    private String URL_JSON;
    private static Context context;
    private static Cliente instance = null;

    public Cliente(Context context) {
        this.context = context;
    }

    public Cliente() {

    }


    @Override
    public void onCreate() {
        super.onCreate();
        context = getContext();
    }

    public static Context getContext() {
        return context;
    }
    public static Cliente getInstance(Context context) {
        if (instance == null) {
            instance = new Cliente(context);
        }
        return instance;
    }
    public Cliente dologin(String s, String s1) {
        URL_JSON = Constantes.URL_JSON + Constantes.URL_API_CLIENTE + Constantes.URL_API_CLIENTE_LOGIN;
        // Mapeo de los pares clave-valor
        HashMap<String, String> parametros = new HashMap();
        parametros.put("nif", s);
        parametros.put("claveSeguridad", s1);
        JsonObjectRequest jsArrayRequest = new JsonObjectRequest(
                Request.Method.POST,
                URL_JSON,
                new JSONObject(parametros),
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.i("Json", response.toString());
                        // Manejo de la respuesta
                        //notifyDataSetChanged();
                    }
                },
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                });
        PatronSingleton.getInstance(getContext()).addToRequestQueue(jsArrayRequest);
        return null;
    }
}
公共类客户机扩展应用程序{
私有字符串URL_JSON;
私有静态语境;
私有静态客户端实例=null;
公共客户(上下文){
this.context=上下文;
}
公共客户(){
}
@凌驾
public void onCreate(){
super.onCreate();
context=getContext();
}
公共静态上下文getContext(){
返回上下文;
}
公共静态客户端getInstance(上下文){
if(实例==null){
实例=新客户(上下文);
}
返回实例;
}
公共客户多洛金(字符串s,字符串s1){
URL\u JSON=constants.URL\u JSON+constants.URL\u API\u CLIENTE+constants.URL\u API\u CLIENTE\u LOGIN;
//帕雷斯克莱夫山谷酒店
HashMap parametros=新的HashMap();
参数put(“nif”,s);
参数put(“claveSeguridad”,s1);
JsonObjectRequest jsArrayRequest=新JsonObjectRequest(
Request.Method.POST,
URL_JSON,
新的JSONObject(参数),
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
Log.i(“Json”,response.toString());
//雷斯普埃斯塔酒店
//notifyDataSetChanged();
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
});
getInstance(getContext()).addToRequestQueue(jsArrayRequest);
返回null;
}
}

您正在将context设置为null,getContext返回该值

试试这个

public class Cliente extends Application {

    protected static Cliente sInstance;
    private RequestQueue mRequestQueue;

    @Override
    public void onCreate() {
        super.onCreate();

        mRequestQueue = Volley.newRequestQueue(this);
        sInstance = this;
    }

    public synchronized static Cliente getInstance() {
        return sInstance;
    }

    public RequestQueue getRequestQueue() {
        return mRequestQueue;
    }
}
另外,不要忘记将这个
应用程序
添加到清单中,就像在应用程序标记中一样

<application
        android:name=".Cliente"
        android:allowBackup="true"
        ....

您正在将context设置为null,getContext返回该值

试试这个

public class Cliente extends Application {

    protected static Cliente sInstance;
    private RequestQueue mRequestQueue;

    @Override
    public void onCreate() {
        super.onCreate();

        mRequestQueue = Volley.newRequestQueue(this);
        sInstance = this;
    }

    public synchronized static Cliente getInstance() {
        return sInstance;
    }

    public RequestQueue getRequestQueue() {
        return mRequestQueue;
    }
}
另外,不要忘记将这个
应用程序
添加到清单中,就像在应用程序标记中一样

<application
        android:name=".Cliente"
        android:allowBackup="true"
        ....


哪一行导致问题?你能告诉我哪个引用是空的吗?我添加了所有导致问题的tracelogWhich行?PatronSingleton.getInstance(getContext()).addToRequestQueue(jsArrayRequest);这是一种使用应用程序的奇怪方式,您应该将这种网络逻辑转移到其他地方。getContext()似乎总是返回null,因为您没有在客户端构造函数调用中实际设置上下文。哪一行导致了问题?你能告诉我哪个引用是空的吗?我添加了所有导致问题的tracelogWhich行?PatronSingleton.getInstance(getContext()).addToRequestQueue(jsArrayRequest);这是一种使用应用程序的奇怪方式,您应该将这种网络逻辑转移到其他地方。getContext()似乎总是返回null,因为您没有在客户端构造函数调用中实际设置上下文,添加另一个应用程序?您需要在应用程序标记下添加
android:name
,如图所示。如果我想添加其他类似的Java类,您的意思是什么?您只能有一个扩展应用程序的类。该类在整个应用程序中共享。我刚刚看到了更新,但我定义了以下内容:添加另一个应用程序?您需要在应用程序标记下添加
android:name
,如图所示。如果我要添加其他类似的Java类,您的意思是什么?您只能有一个扩展应用程序的类。该类在整个应用程序中共享。