gpt4 book ai didi

android - getApplicationContext()' 在非 Activity 中使用 volley 的空对象引用

转载 作者:行者123 更新时间:2023-11-29 14:46:33 24 4
gpt4 key购买 nike

花点时间尝试从 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)

我没有得到修复,我确定是针对上下文 getContext 或类似的,但无法构建一个

public class RestfulOperacional {
Cliente cliente;

public RestfulOperacional() {
cliente = new Cliente();
}

public Object dologin(String s, String s1) {
return cliente.dologin(s, s1);
}
}

然后类尝试解析 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;
}
}

最佳答案

您正在将上下文设置为 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 添加到您的 Manifest 中,就像在 application 标记中一样

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

或者,由于 Android 中的 Application extends Context,您实际上不需要该变量...调用 getApplicationContext() 会返回 Application 对象。


用法 -- 将 Volley 请求移动到您要建立网络连接的 Activity,以便更新该类中的 View 和内容。

public class MainActivity extends AppCompatActivity {

private static String URL_JSON = "http://my.server.com/login";

private ArrayAdapter<String> adapter;
private ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// use android:id="@android:id/list" in ListView XML
lv = (ListView) findViewById(android.R.id.list);

// Simple adapter to show strings
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
lv.setAdapter(adapter);

doLogin("username?", "password?");
}

private void doLogin(String s, String s1) {

HashMap<String, String> parametros = new HashMap<String, String>();
parametros.put("nif", s);
parametros.put("claveSeguridad", s1);

JsonObjectRequest req = 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
adapter.add(response.toString());
adapter.notifyDataSetChanged();
}
},
new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.getMessage());
}
});

// Start the request
Cliente.getInstance().getRequestQueue().add(req);
}

}

关于android - getApplicationContext()' 在非 Activity 中使用 volley 的空对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35300928/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com