21

I am trying to create a context menu item inside a fragment. But I am facing issue with the getMenuInflater() method. It is undefined. I have imported all the necessary packages. Can anyone point out what I am doing wrong here?

Here is my code:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.view.MenuItem;

public class FeaturedFragment extends Fragment {
    public FeaturedFragment() {
    }
    public static final String ARG_SECTION_NUMBER = "section_number";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.featured_fragment,container,false);
        registerForContextMenu(view);
        return view;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
    {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.toggleview_menu, menu);
    }
}

here is my menu.xml

  <menu xmlns:android="http://schemas.android.com/apk/res/android" >    
      <item android:id="@+id/toggle_view"
         android:title="Change to ListView"
         android:icon="@drawable/collections_view_as_grid"
         android:showAsAction="always" />
      <item android:id="@+id/grid_view"
         android:title="Grid View"
         android:orderInCategory="99"
         android:showAsAction="never" />
      <item android:id="@+id/list_view"
         android:title="List View"
         android:orderInCategory="99"
         android:showAsAction="never" />
  </menu>
gotwo
  • 663
  • 8
  • 16
intrepidkarthi
  • 3,104
  • 8
  • 42
  • 75

1 Answers1

47

Change:

MenuInflater inflater = getMenuInflater();

To this:

MenuInflater inflater = getActivity().getMenuInflater();
waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • Thanks. It works. But my menu item is not shown in the view. I have edited my post with the menu xml file. – intrepidkarthi Sep 14 '12 at 12:03
  • aren't you supposed to use `getMenuInflator()` within `onCreateOptionsMenu` inside an `Activity`? – waqaslam Sep 14 '12 at 12:11
  • Developer Guide (http://developer.android.com/guide/topics/ui/menus.html#FloatingContextMenu) says we can have it inside either an activity or fragment. – intrepidkarthi Sep 14 '12 at 12:14
  • Inside a fragment, onCreateOptionMenu passes in a MenuInflater as the second parameter. Answer given is fine. But, if the inflater is provided in the parameters, I would opt for that, personal preference. – Les Jun 12 '14 at 14:55
  • @Les - But this is about inside `onCreateContextMenu`, not `onCreateOptionsMenu`. No `MenuInflater` argument. – Ted Hopp Sep 29 '16 at 21:02