listview搜索:尝试调用虚方法'void android.widget.Filter.filter(java.lang.CharSequence)'

时间:2018-03-25 13:05:52

标签: android android-search

我已经阅读了所有与我的代码中有相同错误的问题, 我不知道什么是错的,请帮帮我

FragmentSearch.java

adapter = new FunDapter<>(SearchFragment.this.getActivity(), itemList, R.layout.search_row, dic);
    lv = (ListView)view.findViewById(R.id.lvSearch);
    searcher = (SearchView) view.findViewById(R.id.Search);
    lv.setAdapter(adapter);

    searcher.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {

            adapter.getFilter().filter(s);
            return false;
        }
    });

错误

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Filter.filter(java.lang.CharSequence)' on a null object reference
                                                                                  at com.example.junamay.matchdeals.Fragments.SearchFragment$5.onQueryTextChange(SearchFragment.java:148)
                                                                                  at android.widget.SearchView.onTextChanged(SearchView.java:1209)
                                                                                  at android.widget.SearchView.access$2000(SearchView.java:95)
                                                                                  at android.widget.SearchView$10.onTextChanged(SearchView.java:1689)
                                                                                  at android.widget.TextView.sendOnTextChanged(TextView.java:8175)

感谢ADVANCE

1 个答案:

答案 0 :(得分:0)

您应该根据对象的类型为适配器定义过滤器 见this