Search code examples
androidkotlinmenuresources

Menu resource not found


I started a new project and it's a problem I haven't encountered yet.

It's a simple job that other times worked, create a menu for the toolbar Android Resource Directory -> Resource Type 'menu'. It seems it doesn't recognize the menu from the resources.

enter image description here

and using import android.R doesn't work because it doesn't recognize the res menu

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>

<item
    android:id="@+id/logout"
    android:title="Logout"
    app:showAsAction="never"
    />

 </menu>

Created a new project by default and there it seems it worked. Any idea what might be?

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding

// Firebase
lateinit var firebaseUser: FirebaseUser
lateinit var myRef: DatabaseReference

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    firebaseUser = FirebaseAuth.getInstance().currentUser!!
    myRef = FirebaseDatabase.getInstance().getReference("Users").child(firebaseUser.uid)

    myRef.addValueEventListener(object : ValueEventListener {
        override fun onDataChange(snapshot: DataSnapshot) {
            val users: Users? = snapshot.getValue(Users::class.java)
            Toast.makeText(this@MainActivity, "User Login : "+ users?.username, Toast.LENGTH_SHORT).show()
        }

        override fun onCancelled(error: DatabaseError) {

        }

    })
}

//Adding logout functionality
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.menu, menu)
    return true
}

}

Project res structure

enter image description here


Solution

  • and using import android.R doesn't work because it doesn't recognize the res menu

    It will not as android.R does not contain the menu subfolder but <insert_your_package_name_here>.R does. You need to import the R with your package name and the menu will be resolved in your project after a project clean & rebuild.