筛选器错误:尝试在空对象引用上调用接口方法'java.util.Iterator java.util.List.iterator()'

时间:2019-04-15 13:37:10

标签: java android

我正在创建具有回收者视图和搜索界面的应用程序

无论我做什么,我都会收到此错误

PostAdapter过滤器接口

public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
                String charString = charSequence.toString();
                if (charString.isEmpty()) {
                    contactListFiltered = questionList;
                } else {
                    List<Posts> filteredList = new ArrayList<>();
                    for (Posts row : questionList) {

                        if (row.getTitle().toLowerCase().contains(charString.toLowerCase()) || row.getExcerpt().contains(charSequence)) {
                            filteredList.add(row);
                        }
                    }

                    contactListFiltered = filteredList;
                }

                FilterResults filterResults = new FilterResults();
                filterResults.values = contactListFiltered;
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                contactListFiltered = (ArrayList<Posts>) filterResults.values;
                notifyDataSetChanged();
            }
        };
    }

questionList和contactListFiltered为List<Posts>

搜索界面

@Override
public boolean onQueryTextSubmit(String query) {
     ListFragment listFragment = new ListFragment();
     PostAdapter postAdapter = new 
     PostAdapter(listFragment.mPosts,getApplicationContext(),false,false);
     postAdapter.getFilter().filter(query);
     return true;
}

@Override
public boolean onQueryTextChange(String query) {
      ListFragment listFragment = new ListFragment();
      PostAdapter postAdapter = new 
      PostAdapter(listFragment.mPosts,getApplicationContext(),false,false);
      postAdapter.getFilter().filter(query);
      return true;
}

在此行调试看到的错误

if (charString.isEmpty()) {

enter image description here

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,请尝试另外检查charString是否为空

if (charString == null || charString.isEmpty()) {