没有为参数项传递值 [英] No value passed for parameter item

查看:40
本文介绍了没有为参数项传递值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有这个代码:

override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_search, menu)
        val searchItem = menu.findItem(R.id.action_search)
        val searchView = MenuItemCompat.getActionView() as SearchView

        //*** setOnQueryTextFocusChangeListener ***
        searchView.setOnQueryTextFocusChangeListener(object : View.OnFocusChangeListener {

            override fun onFocusChange(v: View, hasFocus: Boolean) {

            }
        })

        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {

            override fun onQueryTextSubmit(query: String): Boolean {

                return false
            }

            override fun onQueryTextChange(searchQuery: String): Boolean {
                adapter!!.filter(searchQuery.trim { it <= ' ' })
                tvListAnimal.invalidate()
                return true
            }
        })


        return true
    }

我在这一行中发现了一个错误:val searchView = MenuItemCompat.getActionView() as SearchView.其实这只是代码的开始.确实 Android Studio 告诉我 No value传递给参数 item 但我真的不知道我必须放什么?

And I found a bug in this line : val searchView = MenuItemCompat.getActionView() as SearchView. Actually it is just the beginning of the code. Indeed Android Studio told me No value passed for parameter item but I don't really know what I have to put ?

非常感谢您的帮助!真的!

Thank a lot for your help ! Really !

这里是 menu_search.xml :

EDIT : here is the menu_search.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=".SettingsFragment">
        <item android:id="@+id/action_search"
            android:title="Search"
            android:icon="@drawable/ic_search"
            app:showAsAction="collapseActionView|always"
            app:actionViewClass="android.support.v7.widget.SearchView"
            android:layout_width="wrap_content" />





</menu>

我在 logcat 中找到了这个:

I found this in the logcat :

 java.lang.ClassCastException: android.support.v7.widget.SearchView cannot be cast to android.widget.SearchView

推荐答案

问题在于MenuItemCompat.getActionView() 需要将MenuItem 作为参数传入:

The problem is that MenuItemCompat.getActionView() requires the MenuItem to be passed in as a parameter:

val searchView = MenuItemCompat.getActionView(searchItem) as SearchView

如果这在您使用的 Android 版本中已弃用(如此处所见),您应该改用它:

If this is depracated in the Android version you're using (as seen here), you should use this instead:

val searchView = searchItem.getActionView() as SearchView

这篇关于没有为参数项传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆