android getMenuInflater() in a fragment subclass - cannot resolve method

后端 未结 4 728
萌比男神i
萌比男神i 2021-02-07 19:32

I am trying to inflate a menu in a class that inherits the Fragment class. Here is my OnCreateOptionsMenu() method -

@Override
public bo         


        
4条回答
  •  借酒劲吻你
    2021-02-07 19:59

    Use this code:

    @Override
    public boolean OnCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.forecastfragment, menu) ;
        final MenuItem item = menu.findItem(R.id.forecastID);
    }
    

    where forecastID is the ID of the item in the menu forcastfragment.xml. Also add setHasOptionsMenu(true); in your OnCreateView() so that the fragment will call the method.

    As a side, it's standard practice to include the word 'menu' in your menu file names such as 'forecastfragment_menu.xml'. It avoids confusion.

提交回复
热议问题