尝试调用虚拟方法'android.content.Context android.content.Context.getApplicationContext()'

时间:2018-06-20 16:50:05

标签: java android nullpointerexception android-context

我的代码遇到了一些麻烦,出现了此错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:106)
    at com.uuaba.uuaba.utils.QueryUtils.<init>(QueryUtils.java:23)
    at com.uuaba.uuaba.helper.RVAdapter$1.onClick(RVAdapter.java:132)
    at android.view.View.performClick(View.java:5637)
    at android.view.View$PerformClick.run(View.java:22429)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

从我的RecyclerView适配器中,我想使用以下代码调用另一个类(QueryUtils.java)的方法:

QueryUtils queryUtils = new QueryUtils();
queryUtils.upgPollAnswer("String1", "String2");

这是我的QueryUtils.java:

public class QueryUtils extends Application{

private String tag_string_req = "req_poll_answer_upg";

// SqLite data management
SQLiteHandler db = new SQLiteHandler(getApplicationContext());
HashMap<String, String> user = db.getUserDetails();
private final String userid = user.get("uid");

public void upgPollAnswer(final String idPoll, final String idAnswer){

    StringRequest strReq = new StringRequest(Request.Method.POST, AppConfig.POLL_ANSWER_UPG, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {

            try {

                JSONObject jObj = new JSONObject(response);
                boolean error = jObj.getBoolean("error");

                if (!error) {

                    PollActivity pollActivity = new PollActivity();
                    pollActivity.reloadActivity();

                } else {



                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {



        }
    }) {

        @Override
        protected Map<String, String> getParams() {
            // Posting parameters to loading url
            Map<String, String> params = new HashMap<String, String>();
            params.put("userid", userid);
            params.put("id_poll", idPoll);
            params.put("id_answer", idAnswer);

            return params;

        }

    };

    // Adding request to request queue
    AppVolleyController.getInstance().addToRequestQueue(strReq, tag_string_req);

}

}

错误(QueryUtils.java:23)对应于以下代码行:

SQLiteHandler db = new SQLiteHandler(getApplicationContext());

也许只是我还不了解Context的含义,但是我尝试了不同的解决方案,但没有人能解决!

请帮帮我

1 个答案:

答案 0 :(得分:1)

尝试:

SQLiteHandler db;

@Override 
public void onCreate() {
    super.onCreate() 
    SQLiteHandler db = new SQLiteHandler(getApplicationContext());
     ....rest of db calls..
}