android - 无法使操作栏中的 SearchView 正常工作

标签 android list listview search searchview

我已经尝试了很多教程、stackoverflow 解决方案和谷歌教程 -> https://www.youtube.com/watch?v=9OWmnYPX1uc但我无法让操作栏中的搜索 View 正常工作。

有没有人看到我做错了什么?

我总是在 searchView 上得到一个 nullpointerexception

我的 ListView Activity :

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.list_menu, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setOnQueryTextListener(
            new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    Toast.makeText(ContactListActivity.this, query, Toast.LENGTH_SHORT).show();
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String newText) {
                    Toast.makeText(ContactListActivity.this, newText, Toast.LENGTH_SHORT).show();
                    return false;
                }
            }
    );

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    ComponentName componentName = new ComponentName(getApplicationContext(), TorrentListActivity.class);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName));
    return true;
}

Searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable
    android:label="@string/app_name"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:hint="Search"
    android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"/>

list_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="be.vanlooverenkoen.torrentsearch.TorrentListActivity">

    <item
        android:id="@+id/action_search"
        android:title="Search"
        android:icon="@drawable/ic_action_search"
        app:showAsAction="ifRoom|collapseActionView"
        android:actionViewClass="android.support.v7.widget.SearchView"/>

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/menu_settings"
        app:showAsAction="never" />
</menu>

编辑 1

enter image description here 我不想在 1 个 Activity 上提出建议,但在另一个 Activity 上我需要过滤 ListView 的列表

**编辑 2 **

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.LinkedList;
import be.vanlooverenkoen.torrentsearch.CommonUtilities.CommonUtilities;
import be.vanlooverenkoen.torrentsearch.model.Torrent;
/**
 * Created by Koen on 19/03/2016.
 */
public class TorrentListAdapter extends BaseAdapter {
    private LinkedList<Torrent> torrents;
    private LayoutInflater layoutInflater;
    public TorrentListAdapter(Context context, LinkedList<Torrent> torrents) {
        this.torrents = torrents;
        layoutInflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return torrents.size();
    }
    @Override
    public Object getItem(int position) {
        return torrents.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
            holder = new ViewHolder();
            holder.titleView = (TextView) convertView.findViewById(R.id.txt_title);
            holder.seedView = (TextView) convertView.findViewById(R.id.txt_seedsValue);
            holder.peerView = (TextView) convertView.findViewById(R.id.txt_peersValue);
            holder.sizeView = (TextView) convertView.findViewById(R.id.txt_sizeValue);
            holder.dateView = (TextView) convertView.findViewById(R.id.txt_datevalue);
            holder.torrentTypeTitleView = (TextView) convertView.findViewById(R.id.torrenttypetitle);
            holder.torrentTypeView = (TextView) convertView.findViewById(R.id.torrentTypeView);
            holder.verifiedImg = (ImageView) convertView.findViewById(R.id.verified);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Torrent torrent = torrents.get(position);
        holder.titleView.setText(torrent.getTitle());
        Log.d("Seeds", String.valueOf(holder.titleView.getText()));
        holder.seedView.setText(String.valueOf(torrent.getSeeds()));
        holder.peerView.setText(String.valueOf(torrent.getPeers()));
        holder.sizeView.setText(torrent.getSize());
        holder.dateView.setText(torrent.getDateOfRelease());
        if(CommonUtilities.checkForShowTorrentType(layoutInflater.getContext())){
            holder.torrentTypeView.setText(torrent.getTorrentType());
            holder.torrentTypeTitleView.setText("Torrent Type:");
        }else{
            holder.torrentTypeView.setText("");
            holder.torrentTypeTitleView.setText("");
        }
        if (torrent.isLegitUploader()) {
            holder.verifiedImg.setImageResource(R.drawable.verified);
        }else{
            holder.verifiedImg.setImageDrawable(null);
        }
        return convertView;
    }
    static class ViewHolder {
        TextView titleView;
        TextView seedView;
        TextView peerView;
        TextView sizeView;
        TextView dateView;
        TextView torrentTypeTitleView;
        TextView torrentTypeView;
        ImageView verifiedImg;
    }
}

编辑 3

FATAL EXCEPTION: main
                                                                                  Process: be.vanlooverenkoen.torrentsearch, PID: 10509
                                                                                  java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setOnQueryTextListener(android.support.v7.widget.SearchView$OnQueryTextListener)' on a null object reference
                                                                                      at be.vanlooverenkoen.torrentsearch.TorrentListActivity.onCreateOptionsMenu(TorrentListActivity.java:105)
                                                                                      at android.app.Activity.onCreatePanelMenu(Activity.java:2852)
                                                                                      at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:298)
                                                                                      at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
                                                                                      at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:241)
                                                                                      at android.support.v7.app.AppCompatDelegateImplV7.preparePanel(AppCompatDelegateImplV7.java:1273)
                                                                                      at android.support.v7.app.AppCompatDelegateImplV7.doInvalidatePanelMenu(AppCompatDelegateImplV7.java:1553)
                                                                                      at android.support.v7.app.AppCompatDelegateImplV7.access$100(AppCompatDelegateImplV7.java:89)
                                                                                      at android.support.v7.app.AppCompatDelegateImplV7$1.run(AppCompatDelegateImplV7.java:129)
                                                                                      at android.os.Handler.handleCallback(Handler.java:739)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                      at android.os.Looper.loop(Looper.java:148)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5422)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

解决方案

天呐 android:actionViewClass="android.support.v7.widget.SearchView" 一定是 app:actionViewClass="android.support.v7.widget.SearchView"

然后它起作用了

最佳答案

在你的 list_menu.xml 中,

android:actionViewClass="android.support.v7.widget.SearchView"

应该改为:

app:actionViewClass="android.support.v7.widget.SearchView"

关于android - 无法使操作栏中的 SearchView 正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36428160/

相关文章:

Android:快速查找联系人列表

用于检测移动浏览器屏幕宽度的 Javascript?

list - 如何在Robot Framework中将全局变量或套件变量设置为列表?

python - 计算字符串内列表中的子字符串

android 如何同步 onitemclicklistener listview id 和数据库 id

java - 我正在开发一个简单的英语学习教程应用程序,但我在设置编号位置时遇到了问题

Android 线程 Process.setThreadPriority

android - 如何在 Android 的 Canvas 上绘制平滑/抖动渐变

C++ 有没有办法以同样的效率组合这个列表和映射?

c# - 在 ListView 的特定列中搜索