I have this problem to connect the menu to the main activity:
Modifier 'override' is not applicable to 'local function'
This is the main activity code:
enter code heclass MainActivity : AppCompatActivity() {
private lateinit var adapter: Adapter
private lateinit var layoutManger:LinearLayoutManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
layoutManger = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
adapter = Adapter()
val list = ArrayList<Model>()
list.apply {
add(Model("fatemeh", "chamani fard"))
add(Model("reyhaneh", "chamani fard"))
add(Model("bahareh", "chamani fard"))
add(Model("abolfazl", "chamani fard"))
}
adapter.setList(list)
list_main.layoutManager=layoutManger
list_main.adapter=adapter
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.main_menu,menu)
return super.onCreateOptionsMenu(menu)
}
}
}re
It gives an error on the word OVERRIDE
AND code menu:
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="@ id/menuItemNew"
android:icon="@drawable/icons8_add_100"
android:title="new"
app:showAsAction="ifRoom" />
<item
android:id="@ id/menuItemSearch"
android:icon="@drawable/icons8_search_150"
android:title="search"
app:showAsAction="ifRoom" />
error: Modifier 'override' is not applicable to 'local function'
Help me please :)
CodePudding user response:
You are getting this error because you are creating your onCreateOptionsMenu
function inside your onCreate
making it a local function.
Make sure your onCreateOptionsMenu
function is inside the class instead.
class MainActivity : AppCompatActivity() {
private lateinit var adapter: Adapter
private lateinit var layoutManger:LinearLayoutManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
layoutManger = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
adapter = Adapter()
val list = ArrayList<Model>()
list.apply {
add(Model("fatemeh", "chamani fard"))
add(Model("reyhaneh", "chamani fard"))
add(Model("bahareh", "chamani fard"))
add(Model("abolfazl", "chamani fard"))
}
adapter.setList(list)
list_main.layoutManager=layoutManger
list_main.adapter=adapter
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.main_menu,menu)
return super.onCreateOptionsMenu(menu)
}
}