Skip to content Skip to sidebar Skip to footer

Getapplicationcontext()' On A Null Object Reference Using Volley In Non-activity

Take time trying to call a json from android, but the problem is the confusion of context, this is the error returned me 02-09 19:48:05.494 16350-16350/com.example.cesar.mybank

Solution 1:

You are setting context to null and getContext returns that value.

Try this

publicclassClienteextendsApplication {

    protectedstaticCliente sInstance;
    privateRequestQueue mRequestQueue;

    @OverridepublicvoidonCreate() {
        super.onCreate();

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

    public synchronized staticClientegetInstance() {
        return sInstance;
    }

    publicRequestQueuegetRequestQueue() {
        return mRequestQueue;
    }
}

Also, don't forget to add this Application to your Manifest like so in the application tag

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

Alternatively, since Application extends Context in Android, you really don't need that variable... calling getApplicationContext() returns the Application object.


Usage -- move the Volley request to the Activity where you want to make the network connection in order to update the views and things in that class.

publicclassMainActivityextendsAppCompatActivity {

    privatestaticStringURL_JSON = "http://my.server.com/login";

    privateArrayAdapter<String> adapter;
    privateListView lv;

    @OverrideprotectedvoidonCreate(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 = newArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
        lv.setAdapter(adapter);

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

    privatevoiddoLogin(String s, String s1) {

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

        JsonObjectRequest req = newJsonObjectRequest(
                Request.Method.POST,
                URL_JSON,
                newJSONObject(parametros),
                newResponse.Listener<JSONObject>() {
                    @OverridepublicvoidonResponse(JSONObject response) {
                        Log.i("Json", response.toString());
                        // Manejo de la respuesta
                        adapter.add(response.toString());
                        adapter.notifyDataSetChanged();
                    }
                },
                newResponse.ErrorListener() {

                    @OverridepublicvoidonErrorResponse(VolleyError error) {
                        Log.e("Volley", error.getMessage());
                    }
                });

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

}

Post a Comment for "Getapplicationcontext()' On A Null Object Reference Using Volley In Non-activity"