2017-09-13 79 views
0

我一直在關注如何讓我的checkbox工作在我的listview上S.O的教程/帖子。很多東西我想用它做,但首先我想簡單地得到項目的位置檢查,但應用程序崩潰,我得到一個NullPointerException錯誤。如何獲得在我的listview中工作的複選框?

錯誤的是(下面這部分更多的東西):

java.lang.NullPointerException at com.example.chris.tutorialspoint.SelectPhoneContactAdapter.getView(SelectPhoneContactAdapter.java:104) 

和線路104:

convertView.setTag(v); 

但對我來說,它看起來像我已經正確並按照教程我不知道如何使這些帖子適應我的問題:Getting NullPointerException in custom ListViewCrash in ListView at AbsListView.obtainView for ListActivity。你能告訴我什麼是錯的嗎?一切運行良好,直到我開始嘗試這些複選框。

這裏是我的customadapter代碼,SelectPhoneContactAdapter

public class SelectPhoneContactAdapter extends BaseAdapter { 

    //define a list made out of SelectPhoneContacts and call it theContactsList 
    public List<SelectPhoneContact> theContactsList; 
    //define an array list made out of SelectContacts and call it arraylist 
    private ArrayList<SelectPhoneContact> arraylist; 
    boolean itemChecked[]; 
    Context _c; 

    //define a ViewHolder to hold our name and number info, instead of constantly querying 
    // findviewbyid. Makes the ListView run smoother 
    ViewHolder v; 

    public SelectPhoneContactAdapter(final List<SelectPhoneContact> selectPhoneContacts, Context context) { 
     theContactsList = selectPhoneContacts; 
     _c = context; 
     this.arraylist = new ArrayList<SelectPhoneContact>(); 
     this.arraylist.addAll(theContactsList); 
     itemChecked = new boolean[theContactsList.size()]; 

    } 


    @Override 
    public int getCount() { 
     System.out.println("the amount in arraylist :" + arraylist.size()); 
     return arraylist.size(); 
    } 

    @Override 
    public Object getItem(int i) { 
     return theContactsList.get(i); 
    } 

    @Override 
    public long getItemId(int i) { 
     return i; 
    } 

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) 

    static class ViewHolder { 
     //  In each cell in the listview show the items you want to have 
//  Having a ViewHolder caches our ids, instead of having to call and load each one again and again 
     TextView title, phone; 
     CheckBox check; 
    } 

    @Override 
    public View getView(final int i, View convertView, ViewGroup viewGroup) { 

     //we're naming our convertView as view 
     View view = convertView; 


     if (view == null) { 

      v = new ViewHolder(); 
      System.out.println("getview position :" + i); 

      //if there is nothing there (if it's null) inflate the view with the layout 
      LayoutInflater li = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      view = li.inflate(R.layout.phone_inflate_listview, null); 


      //  So, for example, title is cast to the name id, in phone_inflate_listview, 
//  phone is cast to the id called no etc 
      v.title = (TextView) view.findViewById(R.id.name); 
      v.phone = (TextView) view.findViewById(R.id.no); 
      v.check = (CheckBox) view.findViewById(R.id.checkBoxContact); 

      convertView.setTag(v); 

      //or else use the view (what we can see in each row) that is already there 
     } else { 
      view = convertView; 

     } 


//  store the holder with the view 
     final SelectPhoneContact data = (SelectPhoneContact) theContactsList.get(i); 

     //in the listview for contacts, set the name 
     v.title.setText(data.getName()); 
     //in the listview for contacts, set the number 
     v.phone.setText(data.getPhone()); 
     v.check.setChecked(false); 

     v.check.setChecked(itemChecked[i]); 

     v.check 
       .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
        @Override 
        public void onCheckedChanged(CompoundButton buttonView, 
               boolean isChecked) { 
         itemChecked[i] = isChecked; 
        } 
       }); 

     // Return the completed view to render on screen 
     return view; 


    } 
} 

我的getter和setter方法,SelectPhoneContact

public class SelectPhoneContact { 

    String phone; 

    public String getPhone() {return phone;} 

    public void setPhone(String phone) { 
     this.phone = phone; 
    } 

    String name; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    Boolean selected; 

    public boolean isSelected() { 
     return selected; 
    } 

    public void setSelected(boolean selected){ 
     this.selected=selected; 
    } 
} 

如果必要的話,我可以發佈更多的代碼。

+0

我沒有弄清楚問題,但是,您是否有使用列表視圖而不是回收站視圖的具體原因?如果沒有,我建議你看看回收商的看法,你可能喜歡與它一起工作。 –

回答

1

考慮以下代碼:

@Override 
public View getView(final int i, View convertView, ViewGroup viewGroup) { 
    ... 
    View view = convertView; 

    if (view == null) { 
     ... 
     view = li.inflate(R.layout.phone_inflate_listview, null); 
     ... 
     convertView.setTag(v); 
    } 
    ... 
} 

首先你的convertView值分配給您的view變量。如果爲空,則轉入if聲明,您可以通過li.inflate()view分配新值。

但是,您隨後在此if聲明中稍後參考convertView。儘管你上面寫了view = convertView,但在這一點convertView仍然是null

有兩種方法可以解決這個問題。第一種選擇是簡單地將convertView.setTag(v)更改爲view.setTag(v)。另一種方法是刪除這一行:

View view = convertView; 

,只需改變你引用view任何地方使用convertView代替。沒有必要引入一個新的變量View view;你可以直接使用convertView