用android设置适配器上的空指针

时间:2015-11-27 12:54:09

标签: android

这是一篇新帖提到的问题:Nullpointer on sql query with android

快速历史: - Main.Class - Main_fragment.Class

在Fragment中这段代码:

    private void showList() {
    ArrayList<ContactListItems> contactList = new ArrayList<ContactListItems>();
    contactList.clear();

    String query = "SELECT * FROM "+SqlDbHelper.DATABASE_TABLE;
    Cursor c1 = sqlHandler.selectQuery(query);
    if (c1 != null && c1.getCount() != 0) {
        if (c1.moveToFirst()) {
            do {
                ContactListItems contactListItems = new ContactListItems();

                contactListItems.setID(c1.getString(c1.getColumnIndex("id")));

                contactListItems.setFirstName(c1.getString(c1.getColumnIndex("first name")));

                contactListItems.setSecondName(c1.getString(c1.getColumnIndex("secondname")));

                contactListItems.setBDAY(c1.getString(c1.getColumnIndex("bday")));


                contactList.add(contactListItems);

            } while (c1.moveToNext());
        }
    }
    c1.close();


    ContactListAdapter contactListAdapter = new ContactListAdapter(getActivity(), contactList);
    lvCustomList.setAdapter(contactListAdapter);

}

在最后一行我得到一个空指针。

lvCustomList.setAdapter(contactListAdapter);

这是我的ContactListAdapter.class:

public class ContactListAdapter extends BaseAdapter {

Context context;
ArrayList<ContactListItems> contactList;

public ContactListAdapter(Context context, ArrayList<ContactListItems> list) {
    this.context = context;
    contactList = list;
}

@Override
public int getCount() {

    return contactList.size();
}

@Override
public Object getItem(int position) {

    return contactList.get(position);
}

@Override
public long getItemId(int position) {

    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup arg2) {
    ContactListItems contactListItems = contactList.get(position);


    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.custom_listview_item, null);

    }

    TextView tvfirstname = (TextView) convertView.findViewById(R.id.txtViewFirstNamee);
    tvName.setText(contactListItems.getFirstname());

    TextView tvsecondname = (TextView) convertView.findViewById(R.id.txtViewSecondName);
    tvPhone.setText(contactListItems.getSecondName());

    TextView tvbday = (TextView) convertView.findViewById(R.id.txtViewBDAY);
    tvbday.setText(contactListItems.getBDAY());

    return convertView;
}

有什么想法吗? :/

1 个答案:

答案 0 :(得分:1)

lvCustomList为空....

如果不是这种情况,请发布日志输出并发布初始化lvCustomList的代码。