Errors in onCreateOptionsMenu in navigation search bar

Viewed 118

I’m using Kotlin for a navigation search bar.

So, when I click a button from MainActivity it opens a new activity that enable a simple search can be found.

I’m really struggling as I have a number of errors in the code (I'm a newbe so please be gentle on me!).

The XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<ListView
    android:id="@+id/lv_listView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/tvemptyTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="No result"
    android:textSize="20sp"
    android:visibility="gone" />

</RelativeLayout>

The search code (I've shown the errors within the code:

    class SearchActivity : AppCompatActivity() {

    private lateinit var adapter: ArrayAdapter<*>

    override fun onCreate(savedInstanceState: Bundle?) {
        Log.d("TAG", "SearchActivity 1 JEZ")
        super.onCreate(savedInstanceState)
        setContentView(R.layout.search_main)

        adapter = ArrayAdapter(
            this,
            android.R.layout.simple_list_item_1,
            resources.getStringArray(R.array.vegetables)
        )

        lv_listView.adapter = adapter
        lv_listView.onItemClickListener =
            AdapterView.OnItemClickListener { parent, view, position, id ->
                Toast.makeText(
                    applicationContext,
                    parent?.getItemAtPosition(position).toString(),
                    Toast.LENGTH_SHORT
                ).show()
            }
        lv_listView.emptyView = tvemptyTextView
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.nav_menu,menu)   /* ERROR: Unresolved reference: menu */

        return super.onCreateOptionsMenu(menu)

        val search: MenuItem? = menu?.findItem(R.id.nav_search) /* Unresolved reference: nav_search */
        val searchView: SearchView = search?.actionView as SearchView
        searchView.queryHint = "search something"

        /* Plus a number of errors in this section (which I think maybe related to the earlier problem?)ln assuming : object is not abstract.../Conflicting overload.../ */
        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                return false
            }

            override fun onQueryTextSubmit(newText: String?): Boolean {
                adapter.filter.filter(newText)
                return true
            }
        })

        return super.onCreateOptionsMenu(menu)
    }
}

If I've missed any bits out then I'll add in.

As I've been struggling on this for a few days now, so any help would be great! Cheers

1 Answers

Just a quick tip - you can put the cursor on an error and do Alt+Enter to get some suggestions and automatic quick fixes, and also information about the error. Sometimes it can explain or fix the issue

Ok, so

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.nav_menu,menu)   /* ERROR: Unresolved reference: menu */

I had to paste this into Android Studio to see which menu was highlighted as the unrecognised one - it's the R.menu.nav_menu one. That means you don't have any menu resources yet, i.e. you don't have a menu XML file in a menu folder in your res folder.

So I'm guessing you haven't actually created nav_menu.xml yet. If you open the Project panel (or Alt+1) and in the Android view, find your res folder, right-click it and do New > Android Resource File. Under Resource type select menu and call it nav_menu and it should fix your first error, because now the file exists and gets added to your resources (the R stuff).

/* Unresolved reference: nav_search */

That'll be the same problem - there's nothing with a nav_search ID, so it doesn't recognise that name in your ID list in R. If you didn't create the menu, then you haven't created the menu item with that ID yet right?


object is not abstract.../Conflicting overload...

Two problems here, but caused by the same thing - the longer error message tells you that the object isn't abstract, so there's an abstract method you need to implement, which you haven't yet.

The "conflicting overload" message tells you you have the same method (same name, same parameter types and order, same return type) already implemented.

Basically you have two methods you need to implement, but you've given them the same name - one should be onQueryTextSubmit and the other is onQueryTextChange. They have the right parameter names so the bottom one must have got renamed at some point. Change it to... change and that'll clear up both issues


Also your entire bottom half of that onOptionsMenuCreated method should be highlighted in a warning colour, and if you hover over it it should say "unreachable code". That's because the line before that unreachable code block starts is a return statement, it exits the method there. So that needs to go! And the real return statement (the one at the end) needs to return true

You must return true for the menu to be displayed; if you return false it will not be shown.

Related