java - Volley AppController 类对象返回 null

标签 java android networking android-volley

我正在制作一个应用程序,该应用程序生成 JsonObjectRequest 并使用 Android 的 Volley 网络库从 URL 检索 JSON 数据。

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 类。

并记得更新您的 list 。 IE。使用 <application> 的名称属性在您的 AndroidManifest.xml 中添加此类标签。

<application
        android:name=".AppController"/>

关于java - Volley AppController 类对象返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34479808/

相关文章:

python - 如何检查一个网络是否包含在 Python 中的另一个网络中?

java - 序列生成/广度优先搜索

java - 如何在 ubuntu 10.04 环境中执行命令(带参数)

android - 由于 Android Studio 冲突而忽略了依赖项

networking - Go网络编程库

php - 读取网络驱动器上的文件

java - 如何在关闭应用程序之前停止纹理 View 监听器并显示图像

java - 按属性搜索 OSGI 服务

android - 谷歌地图上的 PolyLines android 用于每个位置更改、新折线或 setPoints?

android - 失败 - 未安装 0