0

我已经尝试了几个小时来让这个简单的搜索功能发挥作用。我已经实现了一些 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() 期间发生了空指针异常!

无论如何,仅从这段代码中可以看出吗?谢谢。

编辑:日志猫

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)
4

0 回答 0