onCreateOptionsMenu已经定义

时间:2018-06-02 12:02:44

标签: java android menu menuitem

我想让我的菜单项可点击以打开其他活动(可能还有活动)。我有两个菜单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(menu。我在这里尝试使用项目来调用另一个活动。请参阅上面的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>

2 个答案:

答案 0 :(得分:0)

您只能在onCreateOptionsMenu和Activity类中只有一个onOptionsItemSelected。要动态更改菜单,请使用invalidateOptionsMenu();无论何时您想要更改它并覆盖此方法来更新菜单

if(A == 5) {
    do_something();
}

if(A == 5) {
    do_some_unrelated_thing();
}

答案 1 :(得分:0)

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