带有 SearchView 的 onQueryListener 不工作

onQueryListener with SearchView not working

我正在尝试使用片段内的支持库在搜索视图上设置侦听器,但出现错误:找不到 onQueryTextListener。我的代码有什么问题吗?

onQueryTextListener绝对存在

Java代码:

package com.example.yagamizero.myapplication;

import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v7.widget.SearchView;

public class ArtistSearchFragment extends Fragment {
   SearchView searchView;
   public ArtistSearchFragment(){

   }
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
       super.onCreate(savedInstanceState);
       getActivity().setContentView(R.layout.fragment_artist_search);
       View rootView = inflater.inflate(R.layout.fragment_artist_search, container, false);

       return rootView;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

        super.onCreateOptionsMenu(menu,inflater);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        searchView = (android.support.v7.widget.SearchView) MenuItemCompat.getActionView(searchItem);
        searchView.setOnQueryTextListener(searchQueryListener);

    }

    private onQueryTextListener searchQueryListener = new onQueryTextListener() {
        @Override
        public boolean onQuerySubmit(String query){

            return true;
        }
        public boolean onQueryTextChange(String newText){

            return true;
        }
    };
}

OnQueryTextListenerSearchView class 中的 嵌套 接口,因此您需要这样定义它:

private SearchView.OnQueryTextListener searchQueryListener = new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        return false;
    }
};