我一直在努力让这个简单的搜索功能起作用。我已经实现了一些performFiltering部分,如下所示:
ContactsFilter filter;
ArrayList<Contact> suggestions;
ArrayList<Contact> seperateSuggestions;
@Override
public Filter getFilter() {
if (filter == null)
filter = new ContactsFilter();
return filter;
}
private class ContactsFilter extends Filter {
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
// TODO Auto-generated method stub
suggestions = (ArrayList<Contact>) results.values;
mFancyAdapter.notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults r = new FilterResults();
if (seperateSuggestions == null) {
synchronized (suggestions) {
seperateSuggestions = new ArrayList<Contact>(suggestions);
}
}
ArrayList<Contact> values = (ArrayList<Contact>)seperateSuggestions;
int count = values.size();
ArrayList<Contact> list = new ArrayList<Contact>();
String prefix = constraint.toString().toLowerCase();
if(constraint != null || constraint.length() == 0) {
synchronized (suggestions) {
ArrayList<Contact> list2 = new ArrayList<Contact>(seperateSuggestions);
r.values = list2;
r.count = list2.size();
}
}
for (int i=0; i<count; i++) {
if( values.get(i).getFullName().toLowerCase().contains(prefix)) {
list.add(suggestions.get(i));
}
}
r.values = list;
r.count = list.size();
return r;
}
}
}
但是我的logcat说在performFiltering()期间发生了一个空指针异常!
有没有告诉我这个代码?感谢。
编辑:logcat
10-05 13:08:31.862: W/Filter(6973): An exception occured during performFiltering()!
10-05 13:08:31.862: W/Filter(6973): java.lang.NullPointerException
10-05 13:08:31.862: W/Filter(6973): at com.example.contactmanager.ListAdapter$ContactsFilter.performFiltering(ListAdapter.java:555)
10-05 13:08:31.862: W/Filter(6973): at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
10-05 13:08:31.862: W/Filter(6973): at android.os.Handler.dispatchMessage(Handler.java:99)
10-05 13:08:31.862: W/Filter(6973): at android.os.Looper.loop(Looper.java:137)
10-05 13:08:31.862: W/Filter(6973): at android.os.HandlerThread.run(HandlerThread.java:60)