You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ListView搜索出现Filter.filter方法调用异常,已查同类问题仍无解求助

Hey there, let's break down that frustrating null pointer error you're hitting! The message Attempt to invoke virtual method 'void android.widget.Filter.filter(java.lang.CharSequence)' tells us one clear thing: the Filter instance you're trying to call filter() on is null. Let's walk through the most common fixes and checks step by step.

1. Verify Your FunDapter Implements Filterable

First off, for any adapter to support filtering with getFilter(), it must implement the android.widget.Filterable interface:

  • If you built FunDapter yourself: Double-check that it extends BaseAdapter (or a subclass) and explicitly implements Filterable. You also need to override getFilter() to return a custom Filter instance that handles your search logic. Without this, getFilter() will default to returning null.
  • If FunDapter is a third-party library: Confirm that the version you're using supports filtering, and that you've configured it correctly. Many third-party adapters require you to specify which data fields should be used for filtering (via your dic parameter, for example) — skip this step, and the adapter won't create a valid Filter instance.

2. Add Null Checks Before Calling filter()

It's safe practice to guard against null values before invoking filter(). Update your SearchView.OnQueryTextListener to include these checks:

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

    @Override
    public boolean onQueryTextChange(String s) {
        filterResults(s);
        return true;
    }

    private void filterResults(String query) {
        // Guard against null adapter or filter
        if (adapter != null && adapter.getFilter() != null) {
            adapter.getFilter().filter(query);
        } else {
            // Log this to debug why the filter is null
            Log.d("SearchDebug", "Adapter or Filter is null! Adapter: " + adapter);
        }
    }
});

This will prevent the crash and give you debug info if the adapter/filter is still null.

3. Validate Your Adapter Initialization

Double-check that your adapter is fully initialized before the SearchView is used:

  • Ensure itemList isn't null or empty when you create the FunDapter — some adapters won't initialize filters if there's no data to filter.
  • Confirm that dic (your data mapping parameter) is correctly set up to include the fields you want to search against. If the adapter doesn't know which fields to filter, it might skip creating the Filter entirely.

4. Debug to Pinpoint the Issue

Add a log statement right after initializing your adapter to check if the filter exists:

adapter = new FunDapter<>(SearchFragment.this.getActivity(), itemList, R.layout.search_row, dic);
Log.d("SearchDebug", "Adapter Filter: " + adapter.getFilter());

If this logs null, you know the problem is with how the adapter is set up (either missing Filterable implementation or incorrect configuration).


内容的提问来源于stack exchange,提问作者Kny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:08:28