0

Hello I have this code :

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
    }

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 !

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>

I found this in the logcat :

 java.lang.ClassCastException: android.support.v7.widget.SearchView cannot be cast to android.widget.SearchView
  • https://stackoverflow.com/questions/24522696/android-widget-searchview-cannot-be-cast-to-android-support-v7-widget-searchview – Zohra Khan Jul 25 '17 at 17:53

1 Answers1

0

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

val searchView = MenuItemCompat.getActionView(searchItem) as SearchView

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
zsmb13
  • 85,752
  • 11
  • 221
  • 226
  • I won't put this in the answer, but is this from a tutorial or something? It's curious that the same code sample keeps coming up here. – zsmb13 Jul 25 '17 at 17:23
  • Actually it comes from the code of Github. But it does not work when I try to do what you tell me the app crashes :/ – Donald Harris Jul 25 '17 at 17:30
  • The exact same code is crashing [here](https://stackoverflow.com/q/45309129/4465208) as well. Same advice as there. You should look at the stacktrace, and post another question if you can't figure it out. – zsmb13 Jul 25 '17 at 17:33
  • It is very strange because I just let the three first lines and it crashes whereas if I let the two first lines it works. The line 3 is really strange :/ – Donald Harris Jul 25 '17 at 17:33
  • Perhaps `getActionView()` is not returning a `SearchView`. In this case, you'd get a `ClassCastException` there. – zsmb13 Jul 25 '17 at 17:33
  • Can you post your menu XML? Do you have the `SearchView` set up there? – zsmb13 Jul 25 '17 at 17:35
  • Yeah, that looks good. Can you get a stacktrace from the crash? – zsmb13 Jul 25 '17 at 17:42
  • 3
    Maybe It is because of this : ` java.lang.ClassCastException: android.support.v7.widget.SearchView cannot be cast to android.widget.SearchView` ? – Donald Harris Jul 25 '17 at 17:49
  • Oh, there you go, you just have the wrong `SearchView` imported in your code. You should import the one from the support library instead of `android.widget.SearchView`. – zsmb13 Jul 25 '17 at 17:50
  • Oh thank you ! Yeah it was that ! Thank you very much ! – Donald Harris Jul 25 '17 at 17:53