How to Create option menu in FragmentActivity?

江枫思渺然 提交于 2019-12-10 20:48:08

问题


Hi Below is the code I am using to create option menu in my FragmentActivity :-

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

    // Menu options to set and cancel the alarm.
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // When the user clicks START ALARM, set the alarm.
            case R.id.start_action:
                alarm.setAlarm(this);
                return true;
            // When the user clicks CANCEL ALARM, cancel the alarm. 
            case R.id.cancel_action:
                alarm.cancelAlarm(this);
                return true;
        }
        return false;
    }

Will anybody tell me why it's not working? It is not affecting app but nothing is happening when I click the option menu button from device. Please Help to resolve this. Thanks in advance!

Below is my main.xml :-

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/start_action"
        android:showAsAction="ifRoom|withText"
        android:title="@string/start_text" />
    <item android:id="@+id/cancel_action"
        android:showAsAction="ifRoom|withText"
        android:title="@string/cancel_text" />
</menu>

回答1:


Return item within Switch case like. ITs Work For me.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        // When the user clicks START ALARM, set the alarm.
        case R.id.start_action:
            alarm.setAlarm(this);
            return true;
        // When the user clicks CANCEL ALARM, cancel the alarm. 
        case R.id.cancel_action:
            alarm.cancelAlarm(this);
            return true;

        default:
            return super.onOptionsItemSelected(item);
      }
}



回答2:


Although this question is old but to close it here is what i believe OP is missing in the code

In the

onCreateOptionsMenu

return it with the super as super.onCreateOptionsMenu(menu);

and in the

onOptionsItemSelected

return it with the super as super.onOptionsItemSelected(item);

all the return type is boolean so you will know it worked correctly when it returns true. Its like simillar to super.onCreate(savedInstancestate).




回答3:


Change

return false;

to

return super.onOptionsItemSelected(item);

as

   @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // When the user clicks START ALARM, set the alarm.
            case R.id.start_action:
                alarm.setAlarm(this);
                return true;
            // When the user clicks CANCEL ALARM, cancel the alarm. 
            case R.id.cancel_action:
                alarm.cancelAlarm(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

Edit:

Also you have to add the following to your Fragment

setHasOptionsMenu(true);


来源:https://stackoverflow.com/questions/23487510/how-to-create-option-menu-in-fragmentactivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!