MenuInflater无效 - 菜单未显示

时间:2014-12-07 18:00:37

标签: android

我的菜单未显示在ActionBar中(onCreateOptionsMenu)被正确调用。图标和字符串可用。代码在我的其他项目中运行良好。我正在使用android.support.v7.app.ActionBarActivity用于android:

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="21" />

代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    Log.i("onCreate", "menu");
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.add, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_add:
            Intent addIntent = new Intent(this, RoomAddActivity.class);
            startActivity(addIntent);
        break;
    }

    return super.onOptionsItemSelected(item);
}

XML:

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

    <item
        android:id="@+id/action_add"
        android:icon="@drawable/ic_action_new"
        android:showAsAction="always"
        android:title="@string/action_add"/>

</menu>

1 个答案:

答案 0 :(得分:0)

尝试明确返回true:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    Log.i("onCreate", "menu");
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.add, menu);
    super.onCreateOptionsMenu(menu);
    return true;
}

<强>更新
顺便说一下,你没有义务打电话给超级方法。至于我的经验,它没有它可以正常工作。