我如何能完成我的构造函数?(How can I complete my constructor?)

2019-10-29 06:09发布

我有这样的代码:

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
    }

这个问题源于此部分:

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

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

   }
})

当我尝试与Android Studio中运行,我得到这个消息:

“这个类没有构造函数。”

我尝试写这样的:

searchView.setOnQueryTextFocusChangeListener(object : View.OnFocusChangeListener {

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

   }
})

它的工作原理,但应用程序崩溃,因为我后删除括号View.OnFocusChangeListener

我不知道如何完成这一点。 有人有一个想法?

文章来源: How can I complete my constructor?