2017-11-11 68 views
-3

在logcat中我有错误:我如何膨胀XML文件,所以我的复选框将停止给我这个NullPointerException错误?

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) 

我的代码的相关部分是:

//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,在错误发生的事情是:

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

checkBoxContact ISN” t在我的activity_view_contact,它在我的phone_inflate_listview,所以我认为这是因为我没有正确地吹胀它,因此e NulPointerException错误。那么,我怎样才能正确地夸大它呢?这是问题,对吧?

我想顺水推舟的下方,但仍然没有工作线:

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

回答

1

你说你尝试这种代码:

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

充气泵工作像你期望。他们夸大了一个观点。

View /*IMPORTANT*/view = layoutInflater.inflate(R.layout.phone_inflate_listview, null); 

但加载这样的观点:

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

你夸大使用了错误的布局/视图,当你像充气这个意思。

而是执行此操作:

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

itemLayout是最有可能无效,因为它未初始化(我说的最有可能的,因为它是一个MCVE,如果它在其他地方或不是因为充气我不能告诉该代码不包括在内)

+0

谢谢我很快就会检查你的建议。我认为itemlayout是好的,因为当我注释掉我的复选框代码时,活动加载好了,itemlayout部分没有isues。 – CHarris

+0

你也可以使用itemLayout,你只需要首先充气/初始化它 – Zoe

+0

你能告诉我充气itemlayout的代码吗?我想'查看视图= itemLayout.inflate(R.layout.phone_inflate_listview,null);'但我得到一个错误,说它期望 'android.content.Context'而不是' R.layout.phone_inflate_listview(int) ',它期望'int',而不是'null',并且它期望一个 root/ViewGroup。我的答案似乎工作正常,虽然我不知道为什么它的工作,宁愿有我的代码中理解的东西。 – CHarris

0

此代码在这里解决了我的问题,i是目前在屏幕上可见的行,所以所有视图都被创建并照顾nullpointerexception,但我更喜欢如果我可以做到这一点通过适当膨胀我的checkbox

int num_of_visible_view=listView.getLastVisiblePosition() - 
            listView.getFirstVisiblePosition(); 

          for (int i = 0; i < num_of_visible_view; 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 = itemLayout.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); 
           } 
          } 
相关问题