为什么我的选项菜单没有正确显示设置菜单项

为什么我的选项菜单没有正确显示设置菜单项

本文介绍了为什么我的选项菜单没有正确显示设置菜单项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自

这是我的代码(与教程相同)的选项菜单部分

This is my code (identical from the tutorial) for the options menu part

@Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // only add the menu when the selection fragment is showing
        if (fragments[SELECTION].isVisible()) {
            if (menu.size() == 0) {
                settings = menu.add(R.string.settings);
            }
            return true;
        } else {
            menu.clear();
            settings = null;
        }
        return false;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        if (item.equals(settings)) {
            showFragment(SETTINGS, true);
            return true;
        }
        return false;
    }

发生的情况是大部分时间工作,但我发现了一个测试案例无法调试当您注销时,菜单项仍然显示,您必须单击它才能使其消失(它不应该显示)。这不是我最开始的一个很大的问题,但是我发现当你点击菜单项后,再次登录,使其消失,选项菜单根本就不会出现。

whats happening is this works most of the time but I found a test case I couldn't debug. When you logout, the menu item still shows and you have to click it to make it disappear.(it shouldn't appear at all). This wasn't a huge concern to me at the beginning but I found that when you logged in again after you clicked the menu item to make it disappear, the options menu doesn't appear at all.

推荐答案

我想我发现了这个问题,我留给别人。从,我得到在Android 3.0及更高版本上,当您要更新菜单时,您必须调用invalidateOptionsMenu(),因为菜单始终打开,系统将调用onPrepareOptionsMenu(),以便您可以更新菜单项。
当我退出时,甚至认为没有实际的菜单项,菜单仍然是开放的。但是当我单击菜单时它是空的,它搞砸了,所以当我登录,显示将无法工作。我还不明白。我希望有人可以详细说明该部分发生的事情。

I think I found the problem, I leave it up for others. From When and how often onPrepareOptionsMenu() method is called for ActionBar? ,I got that "On Android 3.0 and higher, you must call invalidateOptionsMenu() when you want to update the menu, because the menu is always open. The system will then call onPrepareOptionsMenu() so you can update the menu items."When I logged out, even thought there were no actual menu items, the menu was still open. However when I clicked the menu when it was empty, it messed it up so that when I logged in, show wouldn't work. I don't understand that part yet. I was hoping someone can elaborate what happened during that part

这篇关于为什么我的选项菜单没有正确显示设置菜单项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 15:29