Android - 选项菜单不起作用

发布于 2024-09-13 03:46:29 字数 856 浏览 3 评论 0原文

我正在尝试在从服务启动的活动中创建一个选项菜单,然后根据通过处理程序传递的服务消息更改其 UI。

我按如下方式设置“选项”菜单:


     /** Menu creation and setup **/

        /* Creates the menu items */
        public boolean onCreateOptionsMenu(Menu menu) {
            menu.add(0, 1, 0, "Speaker");
            menu.add(0, 2, 0, "Mute");
            return true;
        }

        /* Handles item selections */
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case 1:
                //Do something here
                return true;
            case 2:
               //Do something here
                return true;
            }
            return false;
        }  

但当我的应用程序运行时,它永远不会被调用。

我遇到过问题,因为信息在错误的线程上传递,所以我需要使用处理程序更改屏幕上的文本,同样的问题是否会导致菜单不显示?

那么我该如何修复它,因为我无法重写处理程序中的方法

I am trying to create an Options menu in an Activity that gets started from a Service and then changes its UI based on messages from the Service passed via a Handler.

I set up the Options menu as follows:


     /** Menu creation and setup **/

        /* Creates the menu items */
        public boolean onCreateOptionsMenu(Menu menu) {
            menu.add(0, 1, 0, "Speaker");
            menu.add(0, 2, 0, "Mute");
            return true;
        }

        /* Handles item selections */
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case 1:
                //Do something here
                return true;
            case 2:
               //Do something here
                return true;
            }
            return false;
        }  

But it never gets called when my application is run at all.

I have experienced problems where I need to use a Handler to change Text on the screen as the information is being passed on the wrong thread, could this same issue be the cause of the menu not displaying?

Is so how can I fix it as I cant override a method in a Handler

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羞稚 2024-09-20 03:46:43

尝试在重写的 onCreateOptionsMenu 方法中调用 super 方法,如下所示:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    boolean result = super.onCreateOptionsMenu(menu);
    menu.add(0, 1, 0, "Speaker");
    menu.add(0, 2, 0, "Mute");

    return result;
}

Try calling the super method in your overridden onCreateOptionsMenu method as follows:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    boolean result = super.onCreateOptionsMenu(menu);
    menu.add(0, 1, 0, "Speaker");
    menu.add(0, 2, 0, "Mute");

    return result;
}
野心澎湃 2024-09-20 03:46:42

确保您没有在按键处理程序中使用菜单的按键事件。菜单按钮事件在由 onCreateOptionsMenu 函数处理之前通过键处理程序发送。这可能发生在任何具有焦点的视图中,或者由活动本身发生。解决此问题的快速方法如下:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_MENU)
        return super.onKeyDown(keyCode, event);
    -- insert all other key handling code here --
    return false;
}

Make sure you aren't consuming the key event for the menu in a key handler. The menu button event is sent through key handlers before being handled by the onCreateOptionsMenu function. This could be happening in any view with focus, or by the activity itself. A quick way to fix this would be the following:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_MENU)
        return super.onKeyDown(keyCode, event);
    -- insert all other key handling code here --
    return false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文