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

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

问题描述

您好,我有以下代码:

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 passed for parameter 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天全站免登陆