I'm currently trying to implement a button in my android studio project, using kotlin. I'm not very familiar with kotlin, and so I am referring to this tutorial: https://www.geeksforgeeks.org/popup-menu-in-android-with-example/
I want to create a pop up menu in my android application.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
navigateToMapFragmentIfNeeded(intent)
// mapbox
Mapbox.getInstance(this, getString(R.string.mapbox_access_token));
setContentView(R.layout.activity_main)
/* Referencing and Initializing the button */
var popbutton: Button = findViewById<View>(R.id.clickBtn) as Button
// Setting onClick behavior to the button
popbutton.setOnClickListener(View.OnClickListener { // Initializing the popup menu and
//giving the reference as current context
val popupMenu = PopupMenu(this@MainActivity, popbutton)
// Inflating popup menu from popup_menu.xml file
popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu())
// problem line below
popupMenu.setOnMenuItemClickListener(PopupMenu: OnMenuItemClickListener(){
fun onMenuItemClick(menuItem: MenuItem): Boolean {
// Toast message on menu item clicked
Toast.makeText(
this@MainActivity,
"You Clicked " menuItem.getTitle(),
Toast.LENGTH_SHORT
).show()
return true
}
})
// Showing the popup menu
popupMenu.show()
})
// Request permission
requestPermission()
}
When I try to run in my emulator, I get these errors for the line I have commented on as "problem line below" in the above code:
"unexpected type specification"
"expecting ,"
"expecting an expression"
Could someone tell me how I could fix this issue?
Any and all help is appreciated!! Thanks
CodePudding user response:
Remove the final )
in the last line of the setOnMenuItemClickListener:
popupMenu.setOnMenuItemClickListener(PopupMenu: OnMenuItemClickListener(){
fun onMenuItemClick(menuItem: MenuItem): Boolean {
// Toast message on menu item clicked
Toast.makeText(
this@MainActivity,
"You Clicked " menuItem.getTitle(),
Toast.LENGTH_SHORT
).show()
return true
}
}