onCreateOptionsMenu 已经定义

onCreateOptionsMenu is already defined

我想让我的 菜单项 可以点击,以便打开其他 activity(可能还有其他活动)。我有两个菜单 xml 文件。我已经在 MainActivity.java 中定义了其中之一 main.xml,它正在运行。但后来我添加了另一个带有项目的菜单(作为导航栏)。所以,我也尝试在 MainActivity.java 中定义它。但它在 'onCreateOptionsMenu()''onOptionsItemSelcted()' 上给了我错误。是的,也许两次使用它们很奇怪。但是我应该如何用它的项目定义我的第二个菜单?

MainActivity.java(一段代码):

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.menu_categories:
                if (drawer.isDrawerOpen(r_nav)) {
                    drawer.closeDrawer(r_nav);
                }
                else if (!drawer.isDrawerOpen(r_nav)) {
                    drawer.openDrawer(r_nav);
                }
                if (drawer.isDrawerOpen(navigationView)) {
                    drawer.closeDrawer(navigationView);
                }
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu_item_categ) {
        getMenuInflater().inflate(R.menu.categories_navigation, menu_item_categ);
        return super.onCreateOptionsMenu(menu_item_categ);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.m_item_avto_qismlar) {

            Intent intent = new Intent(this,activity_ombor.class);
            this.startActivity(intent);
            return true;
        }

        if (id == R.id.m_item_homashyo) {

            Intent intent = new Intent(this,activity_ombor.class);
            this.startActivity(intent);
            return true;
        }

        if (id == R.id.m_item_kitob) {

            Intent intent = new Intent(this,activity_ombor.class);
            this.startActivity(intent);
            return true;
        }

        if (id == R.id.m_item_kanstovar) {

            Intent intent = new Intent(this,activity_ombor.class);
            this.startActivity(intent);
            return true;
        }

        if (id == R.id.m_item_libos) {

            Intent intent = new Intent(this,activity_ombor.class);
            this.startActivity(intent);
            return true;
        }

        if (id == R.id.m_item_texnika) {

            Intent intent = new Intent(this,activity_ombor.class);
            this.startActivity(intent);
            return true;
        }

        if (id == R.id.m_item_mebel) {

            Intent intent = new Intent(this,activity_ombor.class);
            this.startActivity(intent);
            return true;
        }

        if (id == R.id.m_item_ovqat) {

            Intent intent = new Intent(this,activity_ombor.class);
            this.startActivity(intent);
            return true;
        }

        if (id == R.id.m_item_materiallar) {

            Intent intent = new Intent(this,activity_ombor.class);
            this.startActivity(intent);
            return true;
        }

        if (id == R.id.m_item_tekstil) {

            Intent intent = new Intent(this,activity_ombor.class);
            this.startActivity(intent);
            return true;
        }

        if (id == R.id.m_item_uskunalar) {

            Intent intent = new Intent(this,activity_ombor.class);
            this.startActivity(intent);
            return true;
        }


        return super.onOptionsItemSelected(item);
    }

更新:

main.xml(菜单。我使用它是为了将图标放在操作栏上,通过单击我可以调用导航菜单)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/menu_categories"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        android:icon="@drawable/ic_action_categories"
        app:showAsAction="ifRoom" />
</menu>

categories_navigation.xml(菜单。这里我试图用项目来调用另一个activity。见上文MainActivity.java)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
        <item android:title="Bo'limlar">
            <menu>
                <item
                    android:id="@+id/m_item_avto_qismlar"
                    android:icon="@drawable/ic_011_car_repair"
                    android:title="Avto-ehtiyot qismlar" />
                <item
                    android:id="@+id/m_item_homashyo"
                    android:icon="@drawable/ic_002_block"
                    android:title="Homashyolar" />
                <item
                    android:id="@+id/m_item_kitob"
                    android:icon="@drawable/ic_009_agenda"
                    android:title="Kitoblar" />
                <item
                    android:id="@+id/m_item_kanstovar"
                    android:icon="@drawable/ic_008_stationery"
                    android:title="Kanstovarlar" />
                <item
                    android:id="@+id/m_item_libos"
                    android:icon="@drawable/ic_007_dress"
                    android:title="Liboslar" />
                <item
                    android:id="@+id/m_item_texnika"
                    android:icon="@drawable/ic_006_transport"
                    android:title="Maxsus texnika" />
                <item
                    android:id="@+id/m_item_mebel"
                    android:icon="@drawable/ic_005_sofa"
                    android:title="Mebellar" />
                <item
                    android:id="@+id/m_item_ovqat"
                    android:icon="@drawable/ic_004_apple"
                    android:title="Oziq-ovqat mahsulotlari" />
                <item
                    android:id="@+id/m_item_materiallar"
                    android:icon="@drawable/ic_003_tools"
                    android:title="Qurilish materiallari" />
                <item
                    android:id="@+id/m_item_tekstil"
                    android:icon="@drawable/ic_010_ball_of_wool"
                    android:title="Tekstil mahsulotlari" />
                <item
                    android:id="@+id/m_item_uskunalar"
                    android:icon="@drawable/ic_001_conveyor"
                    android:title="Uskunalar" />
            </menu>
        </item>
    </group>
</menu>

您的 Activity class 中只能有 onCreateOptionsMenu 和 onOptionsItemSelected。要动态更改菜单,请使用 invalidateOptionsMenu();每当您想更改它并覆盖此方法以更新您的菜单时

@Override
public boolean onPrepareOptionsMenu(Menu menu){
    //code here
}

当我上传问题时,我是初学者。如果遇到这种情况,您可以只创建一个按钮,然后创建一个菜单。当您单击该按钮时,将打开菜单。这意味着,如果其中一个菜单只包含一个项目,则您不需要创建两个菜单。