onCreateOptionsMenu is never called

2019-01-22 05:58发布

I am having some trouble getting an options menu working in Android. I build apps before, and they all worked fine, but now the menu just doesn't pop up.

The code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    getMenuInflater().inflate(R.menu.activity_video, menu);
    return true;
}

the whole method is never even called (checked by setting a breakpoint). The activity is super-simple, it just has a VideoView in it, with an OnTouchListener set.

I am using Android 4.0.4 on a Samsung Galaxy 10.1, API level 15, minSDK 15. Am I missing something?

9条回答
beautiful°
2楼-- · 2019-01-22 06:20

In the method: Fragment#onCreateView(...) you should put:

setHasOptionsMenu(true);

Then your method will be called.

查看更多
来,给爷笑一个
3楼-- · 2019-01-22 06:24

Call setHasOptionsMenu function from onCreate first. The onCreateOptionsMenu will be automatically called.

查看更多
萌系小妹纸
4楼-- · 2019-01-22 06:26

I had the same issue. My problem was solved by inheritance of a different activity class.

So I had been doing:

public class WelcomeActivity extends Activity

but changed this to:

public class WelcomeActivity extends AppCompatActivity

This way I was saying that say an action bar can be added to your activity.

查看更多
登录 后发表回答