0

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(searchItem) 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.toString().trim { it <= ' ' })
                tvListAnimal.invalidate()
                return true
            }
        })


        return true
    }

The problem comes from this part:

searchView.setOnQueryTextFocusChangeListener(object : View.OnFocusChangeListener() {

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

   }
})

When I try to run with Android Studio, I get this message:

'This class does not have a constructor.'

I try to write like this:

searchView.setOnQueryTextFocusChangeListener(object : View.OnFocusChangeListener {

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

   }
})

It works but the app crashes, because I remove the parenthesis after View.OnFocusChangeListener.

I have no idea how to complete this. Someone has an idea?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Ross Thomas
  • 343
  • 1
  • 3
  • 4
  • 1
    your app crashes is not about this problem, you should put the stacktrace here. – holi-java Jul 25 '17 at 16:42
  • 1
    Your second solution is the right one for creating an `OnFocusChangeListener` instance. Since you're not inheriting from a class here, you don't need to call the constructor of the superclass (which is what `()` would mark), you just have to write down the name of the interface there. You should post the log that your crash produces (perhaps as a separate question, since the issue is not about constructors). – zsmb13 Jul 25 '17 at 16:43
  • Thank you but in the first case I mean where `object : View.OnFocusChangeListener() ` I got the message 'This class does not have a constructor.' So for you the solution is to remove the parenthesis ? And the fact that the app crashes is not about this ? – Ross Thomas Jul 25 '17 at 16:48
  • Yes, you have to remove the parenthesis, because you're inheriting from an interface, and not extending a class. And yes, your crash is caused by something else in your code. You should look at the stacktrace to see what crashes your app, and if you can't figure it out, post that error as a separate question. – zsmb13 Jul 25 '17 at 16:49
  • Yes you totally right ! I will go to post ! thank you ! – Ross Thomas Jul 25 '17 at 17:07
  • The same code came up with a crash [here](https://stackoverflow.com/q/45309872/4465208). Did you fix your crash? – zsmb13 Jul 25 '17 at 17:39
  • The other question is now solved. If you still have your crash, you should check out the solution there (it was a wrong import in that case). – zsmb13 Jul 25 '17 at 17:54

0 Answers0