Volley AppController类对象返回null [英] Volley AppController class object returning null
本文介绍了Volley AppController类对象返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在制作一个应用程序,该应用程序使用Android的Volley Networking Library创建一个JsonObjectRequest
并从URL检索JSON数据.
I am making an app that makes a JsonObjectRequest
and retrieves a JSON data from an URL using the Volley Networking Library for android.
AppController.java
public class AppController extends AppCompatActivity {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private static AppController mInstance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mInstance = this;
}
public static synchronized AppController getInstance(){
return mInstance;
}
public RequestQueue getRequestQueue(){
if(mRequestQueue == null){
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
}
MainActivity.class中的方法
private void makeJSONObjectRequest() {
showDialog();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
urlJsonObj, (String) null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
try {
//Parsing JSON Object response
String name = response.getString("name");
String email = response.getString("email");
JSONObject phone = response.getJSONObject("phone");
String home = phone.getString("home");
String mobile = phone.getString("mobile");
jsonResponse = "";
jsonResponse += "Name: " + name + "\n\n";
jsonResponse += "Email: " + email + "\n\n";
jsonResponse += "Home: " + home + "\n\n";
jsonResponse += "Mobile: " + mobile + "\n\n";
txtResponse.setTag(jsonResponse);
}
catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
hideDialog();
}
},
new ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
VolleyLog.d(TAG+"Error:"+ volleyError.getMessage());
Toast.makeText(getApplicationContext(), volleyError.getMessage(), Toast.LENGTH_SHORT).show();
hideDialog();
}
});
/*THE ERROR OCCURS HERE! */
//adding request to the RequestQueue
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
它给出了以下错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.h8pathak.volleyjson.AppController.addToRequestQueue(com.android.volley.Request)' on a null object reference
如何纠正此代码?
推荐答案
您的AppController class
需要扩展Application类而不是AppCompatActivity类.
Your AppController class
needs to extend Application class instead of the AppCompatActivity class.
并且请记住也要更新清单. IE.使用<application>
标签的名称属性将此类添加到您的AndroidManifest.xml中.
And remember to update your Manifest as well. ie. Add this class in your AndroidManifest.xml using name attribute for <application>
tag.
<application
android:name=".AppController"/>
这篇关于Volley AppController类对象返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文