Android - 选项菜单不起作用
我正在尝试在从服务启动的活动中创建一个选项菜单,然后根据通过处理程序传递的服务消息更改其 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在重写的 onCreateOptionsMenu 方法中调用 super 方法,如下所示:
Try calling the super method in your overridden onCreateOptionsMenu method as follows:
确保您没有在按键处理程序中使用菜单的按键事件。菜单按钮事件在由 onCreateOptionsMenu 函数处理之前通过键处理程序发送。这可能发生在任何具有焦点的视图中,或者由活动本身发生。解决此问题的快速方法如下:
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: