onOptionsItemSelected 返回错误的 ID

发布于 2024-11-30 18:08:01 字数 277 浏览 1 评论 0原文

(新手)

当我单击菜单时,上述方法返回第一个菜单中的 ID,而不是我单击的菜单。如果我检查菜单的“Title Condensed”,它是正确的。

int id = item.getItemId();   //returns id of an incorrect menu
String Title = (String) item.getTitleCondensed();  //this returns the correct title.

欢迎任何想法。

(Newbe)

When I click on a menu the above method returns an ID from the first menu, not the one I clicked. If I check for the Title Condensed of the menu it is correct.

int id = item.getItemId();   //returns id of an incorrect menu
String Title = (String) item.getTitleCondensed();  //this returns the correct title.

Any ideas welcome.

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

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

发布评论

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

评论(2

旧夏天 2024-12-07 18:08:01

我也有同样的问题。构建生成的文件未正确更新。

如果我重新排序 xml 中的菜单项,我会得到相同的效果...构建和惊喜。单击菜单会带来超出预期的其他代码。

清理并重试

I had the same problem. Generated files from the build are not properly updated.

I got the same effect if i reordered the menu items in the xml...build and surprise. Clicking on menu brings other codes than expected.

Do a clean and try again

余生共白头 2024-12-07 18:08:01

您应该在 onCreateOptionsMenu 和 onCreateContextMenu 中为每个菜单项设置唯一的 ID。

例如:

    public static final int CONTEXT_MENU_DELETE = Menu.FIRST;
    public static final int CONTEXT_MENU_EDIT = CONTEXT_MENU_DELETE + 1;

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, 
                    ContextMenuInfo menuInfo) {

            menu.add(0, CONTEXT_MENU_DELETE, 1, R.string.delete);
            menu.add(0, CONTEXT_MENU_EDIT, 2, R.string.edit);
    }

    // And then

    @Override
    public boolean onContextItemSelected(MenuItem item) {

            switch(item.getItemId()) {

            case CONTEXT_MENU_DELETE:
                    // Delete item
                    break;

            case CONTEXT_MENU_EDIT:
                    // Edit item
                    break;
            }
    }

onCreateOptionsMenu 和 onOptionsItemSelected 也是如此。每个菜单选项都应该有一个唯一的常量。

添加:

您没有查看本教程吗?
这个想法是一样的。您应该在 menu.xml 中设置不同的 id:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/new_game"
              android:icon="@drawable/ic_new_game"
              android:title="@string/new_game" />
        <item android:id="@+id/help"
              android:icon="@drawable/ic_help"
              android:title="@string/help" />
    </menu>

然后在 onOptionsItemSelected 中使用这些 id:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.new_game:
            newGame();
            return true;
        case R.id.help:
            showHelp();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

查看官方 Android 菜单教程中的这些代码块,并与您自己的代码进行比较。您还可以发布您的 menu.xmlonCreateOptionsMenuonOptionsItemSelected 这样就很容易找出您的问题。

You should have set each menu item a unique ID in onCreateOptionsMenu and onCreateContextMenu.

For example:

    public static final int CONTEXT_MENU_DELETE = Menu.FIRST;
    public static final int CONTEXT_MENU_EDIT = CONTEXT_MENU_DELETE + 1;

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, 
                    ContextMenuInfo menuInfo) {

            menu.add(0, CONTEXT_MENU_DELETE, 1, R.string.delete);
            menu.add(0, CONTEXT_MENU_EDIT, 2, R.string.edit);
    }

    // And then

    @Override
    public boolean onContextItemSelected(MenuItem item) {

            switch(item.getItemId()) {

            case CONTEXT_MENU_DELETE:
                    // Delete item
                    break;

            case CONTEXT_MENU_EDIT:
                    // Edit item
                    break;
            }
    }

The same is for onCreateOptionsMenu and onOptionsItemSelected. You should have a unique constant for every menu option.

Added:

Didn't you check out this tutorial?
The idea is the same. You should set different ids in menu.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/new_game"
              android:icon="@drawable/ic_new_game"
              android:title="@string/new_game" />
        <item android:id="@+id/help"
              android:icon="@drawable/ic_help"
              android:title="@string/help" />
    </menu>

And then use those ids in onOptionsItemSelected:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.new_game:
            newGame();
            return true;
        case R.id.help:
            showHelp();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

Check out these code blocks from official Android Menu tutorial and compare to your own code. You could also publish your menu.xml, onCreateOptionsMenu and onOptionsItemSelected so it would be easy to figure out your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文