Android - Can not use popup in Fragment

Another problem that easily happens with PopupMenus is the calling menu entry is hidden in the three dots (overflow menu). These entries can't have a popupmenu anchored to it.

This is what I had first and got the java.lang.IllegalStateException: MenuPopupHelper cannot be used without an anchor:

<item android:id="@+id/menu_entry_to_show_popupmenu"
 app:showAsAction="ifRoom" />

and what I needed is

<item android:id="@+id/menu_entry_to_show_popupmenu"
 app:showAsAction="always" />

My full popupmenu setup function from an activity begins like this:

PopupMenu popup = new PopupMenu(getActivity(), getActivity().findViewById(R.id.menu_filter));
    popup.getMenuInflater().inflate(R.menu.filter_tasks, popup.getMenu());
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
    [...]
    popup.show();
}

Problem is popup or dialogs are associated with 'Activity' , you need the Activity context here. But this refers to your 'Fragment'.

One way is to pass the Activity context when you create the instance of Fragment or use the getActivity method of the Fragment which will return the activity context.

So change your code like this

     public void popup_window() {
View menuItemView = getView().findViewById(R.id.menu_popup);
PopupMenu popupMenu = new PopupMenu(getActivity(), menuItemView);
popupMenu.getMenuInflater().inflate(R.menu.list_, popupMenu.getMenu());

popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {  
            case R.id.action_ticket:  
                Intent intdn = new Intent(getActivity(),List_Activity.class);
                intdn.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);                   
                startActivity(intdn);
              break;    

            case R.id.action_survey:  
                Toast.makeText(getActivity(),"Under Construction ",Toast.LENGTH_LONG).show();  
                break;      
            case R.id.action_service_request:  
                Toast.makeText(getActivity(),"Under Construction ",Toast.LENGTH_LONG).show();  
                break;  

              default: 
                  break;  

        }  
         return true;
       }
    });
    popupMenu.show();
 }

When I extents activity the popup is working fine. But when I change into extents Fragment I can not use the popup.

you can call findViewById() directly for Activity, however as you are using a Fragment, youo will need a view object to call findViewById(). eg. getView().findViewById();

  1. View - findViewById()

  2. Activity - findViewById()

    getView() --This will return the root view for the fragment, with this you can call findViewById()

new PopupMenu(this, menuItemView);

Here popup menu requires Context, passed as first parameter. You can use this if you are in activity, however in Fragment you need to use getActivity() instead of this

PopupMenu(Context context, View anchor)

new Intent(List_Activity.this,List_Activity.class);

this is wrong, Actually it should be package context and class

packageContext --- A Context of the application package implementing this class.

class --- The component class that is to be used for the intent.

Use getActivity().getApplicationContext() instead of just getApplicationContext() whenever you want to display Toast from Fragment

So your code will look like

public void popup_window() {

View menuItemView = getView().findViewById(R.id.menu_popup);
PopupMenu popupMenu = new PopupMenu(getActivity(), menuItemView);
popupMenu.getMenuInflater().inflate(R.menu.list_, popupMenu.getMenu());

popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {  
            case R.id.action_ticket:  
                Intent intdn = new Intent(getActivity(),List_Activity.class); // Your nxt activity name instead of List_Activity
                intdn.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);                   
                getActivity().startActivity(intdn);
              break;    

            case R.id.action_survey:  
                Toast.makeText(getActivity().getApplicationContext(),"Under Construction ",Toast.LENGTH_LONG).show();  
                break;      
            case R.id.action_service_request:  
                Toast.makeText(getActivity().getApplicationContext(),"Under Construction ",Toast.LENGTH_LONG).show();  
                break;  

              default: 
                  break;  

        }  
         return true;
    }
});
popupMenu.show();
}

UPDATE:

java.lang.IllegalStateException: MenuPopupHelper cannot be used without an anchor

You get this exception because the Anchor view for this popup is null I guess. Hence whenever system tries to show the popup, it gives you this exception.

Just check the tryShow() in MenuPopupHelper

Also refer to this post on SO by Maxim Zaslavsky