Android onCreateOptionsMenu findItem获取null

时间:2016-11-28 09:16:03

标签: android menu menuitem oncreateoptionsmenu

我是Android开发的新手。我用android studio默认项目模板Navigation Drawer Activity创建了一个新项目。

我添加了如下调试信息:

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);

    MenuItem navCamera = menu.findItem(R.id.nav_camera);

    if(navCamera != null) {
        Log.d("MainActivity", "Not null");
    }

    return true;
}

为什么我无法获取菜单项对象?

main.xml中

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never" />

activity_main_drawer.xml

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

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_camera"
        android:icon="@drawable/ic_menu_camera"
        android:title="Import" />
    <item
        android:id="@+id/nav_gallery"
        android:icon="@drawable/ic_menu_gallery"
        android:title="Gallery" />
    <item
        android:id="@+id/nav_slideshow"
        android:icon="@drawable/ic_menu_slideshow"
        android:title="Slideshow" />
    <item
        android:id="@+id/nav_manage"
        android:icon="@drawable/ic_menu_manage"
        android:title="Tools" />
</group>

<item android:title="Communicate">
    <menu>
        <item
            android:id="@+id/nav_share"
            android:icon="@drawable/ic_menu_share"
            android:title="Share" />
        <item
            android:id="@+id/nav_send"
            android:icon="@drawable/ic_menu_send"
            android:title="Send" />
    </menu>
</item>

</menu>

2 个答案:

答案 0 :(得分:0)

如果您想使用菜单项,请使用 onNavigationItemSelected 函数,如下所示。

.container {
  display: inline-block;
  margin: 10px;
  width: 50px;
  height: 50px;
  background-color: red;
  position: relative;

}

.inner {
  top: 5px;
  left: -10px;
  right: -50px;
  bottom: -10px;
  background-color: green;
  position: absolute;
  z-index:-1;
}
.container:not(:first-child) {
  z-index:-2;
}

onCreateOptionsMenu 如下:

<div class="container">
  <div class="inner">
  </div>
</div>
<div class="container">
</div>

答案 1 :(得分:0)

对于上述答案,我很抱歉,我没有准确地阅读您的问题。您可以在NavigationView中找到onCreate()并使用您的lib设置图标

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
    //other stuff here
    ...
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

    // get menu from navigationView
    Menu menu = navigationView.getMenu();

    // find MenuItem you want to change
    MenuItem nav_camara = menu.findItem(R.id.nav_camara);

    // set new title to the MenuItem
    nav_camara.setTitle("NewTitleForCamera");
    // Set your icon menu here

    // do the same for other MenuItems
    ....

    // add NavigationItemSelectedListener to check the navigation clicks
    navigationView.setNavigationItemSelectedListener(this);

}