Android - 无法在片段中使用弹出窗口 [英] Android - Can not use popup in Fragment

查看:38
本文介绍了Android - 无法在片段中使用弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是安卓新手.现在我正在尝试创建一个滑块.现在我创建了一个滑块.现在我想在单击操作栏中的图标时显示弹出窗口.当我扩展活动时,弹出窗口工作正常.但是当我更改为片段片段时,我无法使用弹出窗口.请让我知道片段页面中弹出的任何想法或示例.

public void popup_window() {查看 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) {开关(item.getItemId()){案例 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);开始活动(intdn);休息;案例 R.id.action_survey:Toast.makeText(getApplicationContext(),"建设中",Toast.LENGTH_LONG).show();休息;案例 R.id.action_service_request:Toast.makeText(getApplicationContext(),"建设中",Toast.LENGTH_LONG).show();休息;默认:休息;}返回真;}});popupMenu.show();}

我收到错误:-

很多错误.请帮我解决这个问题.提前致谢.

LogCat 错误信息:-

 致命异常:主要java.lang.IllegalStateException:MenuPopupHelper 不能在没有锚的情况下使用在 com.android.internal.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:101)在 android.widget.PopupMenu.show(PopupMenu.java:108)在 com.example.sample.Testing_Fragment1.popup_window(Testing_Fragment1.java:262)在 com.example.sample.Testing_Fragment1.onOptionsItemSelected(Testing_Fragment1.java:227)在 android.app.Fragment.performOptionsItemSelected (Fragment.java:1801)在 android.app.FragmentManagerImpl.dispatchOptionsItemSelected (FragmentManager.java:1959)在 android.app.Activity.onMenuItemSelected(Activity.java:2551)在 com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:980)在 com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)在 com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)在 com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)在 com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:547)在 com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)在 android.view.View.performClick(View.java:4204)在 android.view.View$PerformClick.run(View.java:17355)在 android.os.Handler.handleCallback(Handler.java:725)在 android.os.Handler.dispatchMessage(Handler.java:92)在 android.os.Looper.loop(Looper.java:137)在 android.app.ActivityThread.main(ActivityThread.java:5041)在 java.lang.reflect.Method.invokeNative(Native Method)在 java.lang.reflect.Method.invoke(Method.java:511)在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)在 dalvik.system.NativeStart.main(本机方法)

解决方案

当我扩展活动时,弹出窗口工作正常.但是当我改变进入范围片段我无法使用弹出窗口.

您可以直接为 Activity 调用 findViewById(),但是当您使用 Fragment 时,您将需要一个视图对象来调用findViewById().例如.getView().findViewById();

  1. 查看 - findViewById()

  2. 活动 - findViewById()

    getView() --这将返回片段的根视图,您可以调用 findViewById()

<块引用>

new PopupMenu(this, menuItemView);

这里的弹出菜单需要Context,作为第一个参数传递.如果你在活动中,你可以使用 this,但是在 Fragment 中你需要使用 getActivity() 而不是 this

PopupMenu(上下文上下文,查看锚点)

<块引用>

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

这是错误的,其实应该是包上下文和类

packageContext --- 实现该类的应用程序包的上下文.

class --- 用于意图的组件类.

当你想从 Fragment<显示 Toast 时,使用 getActivity().getApplicationContext() 而不仅仅是 getApplicationContext()/代码>

所以你的代码看起来像

public void popup_window() {查看 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) {开关(item.getItemId()){案例 R.id.action_ticket:Intent intdn = new Intent(getActivity(),List_Activity.class);//你的 nxt 活动名称而不是 List_Activityintdn.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);getActivity().startActivity(intdn);休息;案例 R.id.action_survey:Toast.makeText(getActivity().getApplicationContext(),"建设中",Toast.LENGTH_LONG).show();休息;案例 R.id.action_service_request:Toast.makeText(getActivity().getApplicationContext(),"建设中",Toast.LENGTH_LONG).show();休息;默认:休息;}返回真;}});popupMenu.show();}

更新:

<块引用>

java.lang.IllegalStateException:无法使用 MenuPopupHelper没有锚

您会收到此异常,因为我猜此弹出窗口的 Anchor 视图为空.因此,每当系统尝试显示弹出窗口时,它都会给您这个异常.

只需检查 tryShow() 中的MenuPopupHelper

另请参阅 Maxim Zaslavsky

在 SO 上发表的这篇文章

I am new to android. Now I am trying to create a slider bar. Now I have created a slider bar. Now I want to show popup when I click the icon in Action bar. When I extents activity the popup is working fine. But when I change into extents Fragment I can not use the popup. Please let me know any idea or any example for popup in Fragment 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 am getting error:-

Lots of error. Please help me to solve this issue. 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)

解决方案

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

这篇关于Android - 无法在片段中使用弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆