onCreateOptionsMenu没有调用,我正在尝试在“儿童片段”中添加菜单
override fun onCreate(savedInstanceState: Bundle?) {
if(activity is AppCompatActivity){
(activity!! as AppCompatActivity).setSupportActionBar(toolbarCarpets)
}
setHasOptionsMenu(true)
super.onCreate(savedInstanceState)
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
Log.d(LOG_TAG,"onCreateOptionsMenu")
inflater.inflate(R.menu.search,menu)
val item : MenuItem = menu.findItem(R.id.action_search)
searchView.setMenuItem(item)
super.onCreateOptionsMenu(menu, inflater)
}
答案 0 :(得分:0)
与其在onCreate()方法中添加工具栏,不如在onActivityCreated()内部添加相同的工具
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
if(activity is AppCompatActivity){
(activity!! as AppCompatActivity).setSupportActionBar(toolbarCarpets)
}
setHasOptionsMenu(true)
}
我猜这里的问题是,当您在onCreate()中添加时,“工具栏”视图对象为null。您可以调试以检查Oncreate()中 toolbarCarpets 的值。这可能是由于在子片段布局中添加了工具栏,直到OnCreateView()方法才被夸大。