Android - unable to use popup in snippet

I am new to android. Now I'm trying to create a slider. Now I have created a slider. Now I want to show a popup when I click the icon in the action bar. When I expand the activity, the popup works fine. But when I change the fragments of the fragment, I cannot use the popup. Please let me know any idea or any example for a popup on a snippet page.

public void popup_window() {
    View menuItemView = findViewById(R.id.menu_popup);
    PopupMenu popupMenu = new PopupMenu(this, 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(List_Activity.this,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(getApplicationContext(),"Under Construction ",Toast.LENGTH_LONG).show();  
                    break;      
                case R.id.action_service_request:  
                    Toast.makeText(getApplicationContext(),"Under Construction ",Toast.LENGTH_LONG).show();  
                    break;  

                  default: 
                      break;  

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

I get an error: - Error image

A lot of mistakes. Please help me solve this problem. Thanks in advance.

LogCat Error Message: -

    FATAL EXCEPTION: main
java.lang.IllegalStateException: MenuPopupHelper cannot be used without an anchor
at com.android.internal.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:101)
at android.widget.PopupMenu.show(PopupMenu.java:108)
at com.example.sample.Testing_Fragment1.popup_window(Testing_Fragment1.java:262)
at com.example.sample.Testing_Fragment1.onOptionsItemSelected(Testing_Fragment1.java:227)
at android.app.Fragment.performOptionsItemSelected(Fragment.java:1801)
at android.app.FragmentManagerImpl.dispatchOptionsItemSelected(FragmentManager.java:1959)
at android.app.Activity.onMenuItemSelected(Activity.java:2551)
at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:980)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:547)
at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
+4
source share
6 answers

, . .

findViewById() Activity, , Fragment, findViewById() . .. GetView() findViewById();

PopupMenu (, menuItemView);

Context, . this, , Fragment getActivity() this

PopupMenu ( , )

Intent (List_Activity.this, List_Activity.class);

,

packageContext --- , .

class --- , .

getActivity().getApplicationContext() getApplicationContext() , Toast Fragment

,

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

, Anchor . , , , .

tryShow() MenuPopupHelper

SO

+11

: getApplicationContext()

: My_Activity.this

0

getActivity().getApplicationContext() .

0

, "Activity", Activity. "".

- Activity getActivity Fragment, .

     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();
 }
0

:

 PopupMenu popupMenu = new PopupMenu(getActivity, menuItemView);
0

Another problem that easily happens with PopupMenus is that the call menu item is hidden at three points (overflow menu). These entries cannot be tied to popupmenu.

This is what I did first and received the java.lang.IllegalStateException: MenuPopupHelper exception cannot be used without binding:

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

and what i needed

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

My full function of setting a popup menu from an action starts as follows:

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();
}
0
source

Source: https://habr.com/ru/post/1539746/


All Articles