Volley AppController类对象返回null

时间:2015-12-27 11:08:29

标签: java android networking android-volley

我正在创建一个使用$value = ""; Excel::load(Input::file('excel'), function ($reader) use ($request, &$value) { $sheets = $reader->get(); foreach($sheets as $sheet) { $value = $error; } } echo $value; 的应用程序,并使用适用于Android的Volley Networking Library从URL检索JSON数据。

AppController.java

JsonObjectRequest

MainActivity.class中的方法

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);
    }
}

它出现以下错误:

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);
    }

如何纠正此代码?

4 个答案:

答案 0 :(得分:7)

您的AppController class需要扩展Application类而不是AppCompatActivity类。

并记得更新你的清单。即。使用<application>标记的名称属性在AndroidManifest.xml中添加此类。

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

答案 1 :(得分:2)

你不能像单身人士一样使用ActivityActivity是您应用的屏幕,在您使用应用期间可能处于不同的状态。你也在泄漏它,因为你保留了它的静态引用。出于您的目的,如果您需要上下文,请扩展Application而不是AppCompatActivity,并在您的清单中注册它。

答案 2 :(得分:2)

我认为您应该像这样创建“AppController”:

public class AppController {

    private static AppController mInstance;

    private RequestQueue mRequestQueue;

    private static Context mCtx;

    private AppController(Context context){
        mCtx = context;
        mRequestQueue = getRequestQueue();
    }

    public static synchronized AppController getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new AppController(context);
        }
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
        }
        return mRequestQueue;
    }

    public <T> void addToRequestQueue(@NonNull final Request<T> request) {
        getRequestQueue().add(request);
    }

    public <T> void addToRequestQueueWithTag(@NonNull final Request<T> request, String tag) {
        request.setTag(tag);
        getRequestQueue().add(request);
    }
}

和MainActivity.class

//adding request to the RequestQueue
AppController.getInstance(this).addToRequestQueue(jsonObjReq);

答案 3 :(得分:1)

不要忘记初始化RequestQueue对象。您需要初始化RequestQueue方法中的onCreate,就像您在示例中看到的那样:
(另外,当您致电request.add(jsonObjectRequest)时,应用程序将尝试引用null对象)

RequestQueue request;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //request qwe
    request= Volley.newRequestQueue(this);
}