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

The signature of your onCreateOptionsMenu doesn’t look right. Take a look at the docs here

Take a look this code

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);//Make sure you have this line of code.
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // TODO Add your menu entries here
    super.onCreateOptionsMenu(menu, inflater);
}

In your fragment class add:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.[IDMENU], menu) ;
}

Where [IDMENU] is the XML name of your menu.

Next you need to add inside onCreate or onCreateView method this:

setHasOptionsMenu(true);

Leave a Comment