2017-09-13 77 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