samedi 11 novembre 2017

How do I inflate xml file so my checkbox will stop giving me this NullPointerException error?

In logcat I have the error:

E/AndroidRuntime: FATAL EXCEPTION: main
                  java.lang.NullPointerException
                      at com.example.chris.tutorialspoint.ViewContact$1.onResponse(ViewContact.java:263)
                      at com.example.chris.tutorialspoint.ViewContact$1.onResponse(ViewContact.java:149)
                      at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
                      at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
                      at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
                      at android.os.Handler.handleCallback(Handler.java:730)
                      at android.os.Handler.dispatchMessage(Handler.java:92)
                      at android.os.Looper.loop(Looper.java:137)
                      at android.app.ActivityThread.main(ActivityThread.java:5419)
                      at java.lang.reflect.Method.invokeNative(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:525)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
                      at dalvik.system.NativeStart.main(Native Method)

The relevant part of my code is:

//stuff here

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(activity_view_contact);

    //stuff here....

    StringRequest stringRequest = new StringRequest(Request.Method.POST, ViewContact_URL,
            new Response.Listener<String>() {

        @Override
                public void onResponse(String response) {

                    //toast the response of ViewContact.php, which has been converted to a
                    //JSON object by the Php file with JSON encode
                    Toast.makeText(ViewContact.this, "OnResponse is" + response, Toast.LENGTH_LONG).show();
                    System.out.println("ViewContact: And the response is " + response);


            try {

                //stuff here


                //we are going to loop through the checked contacts and check the boxes
                //(we could just set them as checked without a loop but User Phone Number,
                //which isn't in the Contacts list, is problematic)
                int count = checkedContactsAsArrayList.size();

                for (int i = 0; i < count; i++) {

                    LinearLayout itemLayout = (LinearLayout) listView.getChildAt(i); // Find by under LinearLayout

                    //inflate the layout that contains the checkbox or else we can get errors
                    LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    View view = layoutInflater.inflate(R.layout.phone_inflate_listview, null);
                    //associate it with a checkbox
                    CheckBox checkbox = (CheckBox) itemLayout.findViewById(R.id.checkBoxContact);
                    //get the other data related to that checkbox - name and number of contact
                    SelectPhoneContact data = (SelectPhoneContact) checkbox.getTag();
                    //if the contact is in the checked array
                    if (checkedContactsAsArrayList.contains(data.getPhone())) {
                        //check the box
                        checkbox.setChecked(true);
                    }
                }



                        //System.out.println("heree it is" + jsonResponse);
                        //Toast.makeText(ContactView.this, jsonResponse, Toast.LENGTH_LONG).show();
                        hidePDialog();

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(),
                                "Error: " + e.getMessage(),
                                Toast.LENGTH_LONG).show();
                    }



                }


            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(ViewContact.this, error.toString(), Toast.LENGTH_LONG).show();

                }

            }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            //we are posting review_id into our ViewContact.php file, which
            //we get when a row is clicked in populistolistview
            //to get matching details
            params.put("review_id", review_id);
            return params;

        }

    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);


     //stuff here

}

Line 263, where the error is happening is:

 CheckBox checkbox = (CheckBox) itemLayout.findViewById(R.id.checkBoxContact);

checkBoxContact isn't in my activity_view_contact, it's in my phone_inflate_listview, so I reckon it's because I'm not inflating it properly, therefore the NulPointerException error. So, how can I inflate it properly? That is the problem, right?

I tried things along the lines of the below but still not working:

                        //inflate the layout that contains the checkbox or else we can get errors
                        LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        View view = layoutInflater.inflate(R.layout.phone_inflate_listview, null);
                        //associate it with a checkbox
                        CheckBox checkbox = (CheckBox) itemLayout.findViewById(R.id.checkBoxContact);




Aucun commentaire:

Enregistrer un commentaire