我开发了一个显示所有 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;
}
谁能建议我我在哪里做错了?