本文介绍了finditem()找不到菜单,停留在NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在更改onCreateOptionsMenu()的选项菜单上的某些属性时,我陷入了困境.似乎findItem()返回null,即使我非常确定对菜单项的引用是正确的也是如此.我的代码如下:

I'm stuck while changing some properties on my options menu at onCreateOptionsMenu(). It seems like findItem() returns null, even though I'm pretty sure that the reference to the menu item is correct. My code looks as follows:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_profile, menu);
        MenuItem leftie = menu.findItem(R.id.menu_profile);
        leftie.setIcon(R.drawable.ic_menu_mapmode);
        leftie.setTitle(R.string.back_map);
        leftie.setIntent(authIntent);

        return true;
    }

我真的不知道那里可能出什么毛病.在此先感谢:)

I really don't know what can be wrong there. Thanks in advance :)

我忘了包括实际问题.

推荐答案

您可以使用XML提及该菜单项的标题和图像.

You can mention title and image for that menu item in XML.

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/newsItem"
    android:icon="@drawable/news_tab"
    android:title="@string/menu_news"/>

<item
    android:id="@+id/dryiceItem"
    android:icon="@drawable/dryice_tab"
    android:title="@string/menu_dryice"/>

</menu>

并可以像这样在menuItem上设置意图:

and can set intent on menuItem like this:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    case R.id.newsItem:
        // start News activity
        //write your intent here.
        break;
    case R.id.dryiceItem:
                   //start another activity
                   //write your intent here.
         break;
            }
    }

这篇关于finditem()找不到菜单,停留在NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 02:32