滚动后ListView中的项目onItemClick抛出NullPointerException

时间:2012-04-19 13:46:21

标签: android listview nullpointerexception custom-adapter onscroll

我开发了一个活动,显示Android手机的所有联系人,我使用ListView显示检索到的联系人。问题是,直到我不滚动ListView,每当我点击任何列表项时 - 它显示特定的联系人记录详细信息。但是,当我滚动并单击列表中的任何项目时,应用程序崩溃并抛出NullPointerException。任何人都可以指导我哪里出错,以及如何实现我的结果。这可能是一个重复的问题,但我尝试在网上搜索没有成功。以下是我的活动的代码段,其中显示了联系人:

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contact_manager);

    filterText = (EditText) findViewById(R.id.search_box);     
    filterText.addTextChangedListener(filterTextWatcher); 

    mContactList = (ListView) findViewById(R.id.contactList);
    populateContactList();      

}

private void populateContactList()
{
    // Another class used to retrieve contacts
              ContactAPI contactAPI = ContactAPI.getAPI();
    ContentResolver cr = getContentResolver();
    contactAPI.setCr(cr);

    ArrayList<Contact> a = new ArrayList<Contact>();
    a = contactAPI.newContactList(this).getContacts();

    contactsAL = new ArrayList<Contact>(a);     

    adapter = new CustomAdapter(this, R.layout.contact_entry, a);       

    mContactList.setAdapter(adapter);   

    mContactList.setOnItemClickListener(new OnItemClickListener(){
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {               

               ***// The below is the line where I am getting NullPointerException***
        LinearLayout l = (LinearLayout) parent.getChildAt(position);
        TextView tv = (TextView) l.getChildAt(1);

        Intent myIntent = new Intent(view.getContext(), ContactDetailsActivity.class);

        Bundle b = new Bundle();
        b.putString("ContactID", tv.getText().toString());
        myIntent.putExtras(b);

        startActivityForResult(myIntent, 0);
    }
    });



private TextWatcher filterTextWatcher = new TextWatcher() 
{      
    public void afterTextChanged(Editable s) 
    {     

    }      
    public void beforeTextChanged(CharSequence s, int start, int count, int after) 
    {     

    }      
    public void onTextChanged(CharSequence s, int start, int before, int count) 
    {         
        adapter.getFilter().filter(s);
    }  
};

protected void onDestroy() 
{     
    super.onDestroy();     
    filterText.removeTextChangedListener(filterTextWatcher); 
}   

以下是我的自定义适配器类代码段:

private ArrayList<Contact> original; 
private ArrayList<Contact> fitems;
private Filter filter;

private LayoutInflater mInflater;

public CustomAdapter(Context context, int layoutResourceId, ArrayList<Contact> data) 
{
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.original = new ArrayList<Contact>(data);         
    this.fitems = new ArrayList<Contact>(data);
}   

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) 
{       
    ViewHolder holder;

    if (convertView == null) 
    {
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);         
        convertView = mInflater.inflate(R.layout.contact_entry, null);
        holder = new ViewHolder();          
        holder.txtName = (TextView) convertView.findViewById(R.id.contactEntryText);
        holder.txtID = (TextView) convertView.findViewById(R.id.contactID);

        convertView.setTag(holder);         
    } 
    else 
    {           
        holder = (ViewHolder) convertView.getTag();         
    }

    if(fitems != null)
    {
        Contact ct = fitems.get(position);
        if(ct != null)
        {
            holder.txtName.setText((String)ct.getDisplayName());
            holder.txtID.setText((String)ct.getId());
        }
    }

    //this.notifyDataSetChanged();

    return convertView;
}

@Override
public Filter getFilter() {
    // TODO Auto-generated method stub
    // return super.getFilter();

    if (filter == null)         
        filter = new ContactFilter();      

    return filter;
}


private class ContactFilter extends Filter 
{ 
    @Override 
    protected FilterResults performFiltering(CharSequence constraint) 
    { 
        FilterResults results = new FilterResults(); 
        String prefix = constraint.toString().toLowerCase(); 

        if (prefix == null || prefix.length() == 0) 
        { 
            ArrayList<Contact> list = new ArrayList<Contact>(original); 
            results.values = list; 
            results.count = list.size(); 
        } 
        else 
        { 
            final ArrayList<Contact> list = new ArrayList<Contact>(original); 
            final ArrayList<Contact> nlist = new ArrayList<Contact>(); 
            int count = list.size(); 

            for (int i=0; i<count; i++) 
            { 
                final Contact cont = list.get(i); 
                final String value = cont.getDisplayName().toLowerCase(); 

                if (value.contains(prefix)) 
                { 
                    nlist.add(cont); 
                } 
            } 
            results.values = nlist; 
            results.count = nlist.size(); 
        } 
        return results; 
    } 

    @SuppressWarnings("unchecked") 
    @Override 
    protected void publishResults(CharSequence constraint, FilterResults results) 
    { 
        fitems = (ArrayList<Contact>)results.values; 

        clear(); 
        int count = fitems.size(); 
        for (int i=0; i<count; i++) 
        { 
            Contact cont = (Contact)fitems.get(i); 
            add(cont); 
        } 
    } 
}

static class ViewHolder {
    TextView txtName;
    TextView txtID;
}

任何人都可以建议我在哪里做错了吗?

2 个答案:

答案 0 :(得分:0)

onItemClick调用自定义适配器。getItem(int)以检索位置上的Contact对象,使用联系人来检索启动新活动所需的信息。

答案 1 :(得分:0)

删除主要活动中的onitemclicklistener设置。

在自定义适配器中,在getView方法中,您只需将onclicklistener添加到textview

即可
holder.txtName.setOnclickListener(new OnClickListener() {

   @Override
  public void onClick() {
  Intent myIntent = new Intent(view.getContext(), ContactDetailsActivity.class);

    Bundle b = new Bundle();
    b.putString("ContactID", holder.txtName.getText().toString());
    myIntent.putExtras(b);

    startActivityForResult(myIntent, 0);

   }
})