0

我開發了一個活動,顯示Android手機的所有聯繫人,我使用ListView來顯示檢索到的聯繫人。問題是,直到我不滾動我的ListView,每當我點擊任何列表項 - 它顯示特定的聯繫人記錄詳細信息。但是,當我滾動並單擊列表中的任何項時,應用程序崩潰並拋出NullPointerException。任何人都可以指導我哪裏出錯,以及如何實現我的結果。這可能是一個重複的問題,但我試圖在網上搜索沒有成功。下面是顯示的聯繫人我的活動的代碼片段:onItem點擊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; 
} 

任何人都可以請建議我我在哪裏做錯了?

回答

0

int onItemClick調用CustomAdapter。 getItem(int)來檢索Contact對象的位置,使用聯繫人來檢索啓動新活動所需的信息。

+0

謝謝@blackbelt,爲您指導。我已經像你建議的那樣做了,而且工作正常。非常感謝 – 2012-04-19 15:21:05

0

在您的主要活動中刪除設置onitemclicklistener。

在CustomAdapter,getView方法中你只需要添加clicklistener到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); 

    } 
}) 
+0

謝謝@Raghu Nagaraju,爲你的指針。將嘗試你的方法也會更新你。再次感謝.. – 2012-04-19 15:22:11