-4

I am going to inflate a menu in my first Activity.

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

The R.menu.main:

<item
    android:id="@+id/action_serach"
    android:title="Search"
    android:orderInCategory="100"
    android:icon="@drawable/ic_search"
    app:showAsAction="ifRoom" />

It doesn't work, but it inflates the another XML file named R.menu.menu_home. The R.menu.menu_home:

<menu 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_star"
android:title="Search"
android:orderInCategory="100"
android:icon="@drawable/ic_star_half"
app:showAsAction="ifRoom"
     />
</menu>

But in my second Activity, it works, whether it's R.menu.main or R.menu.menu_home. why?

zhan
  • 85
  • 1
  • 1
  • 9

3 Answers3

1

I think the problem is that you have to Override the class method, so add @Override before. Here you can get more info about the Override annotation.

hegocre
  • 337
  • 3
  • 8
  • And what if you change the name of the menu, or if you create a new xml menu file with the same contents? – hegocre Jun 11 '17 at 15:12
  • When i change the name of the menu or create a new xml menu file with the same contents,it remain inflates the preview XML file . – zhan Jun 11 '17 at 15:29
0

I have solve this problem.It's because i replace the framelayout with a fragment in the activity and the onCreateOptionsMenu() method is in the fragment.

zhan
  • 85
  • 1
  • 1
  • 9
-2

Use this code.

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
     MenuInflater inflater = getMenuInflater();
     inflater.inflate(R.menu.main, menu);
     return true;
 }
Anik Dey
  • 616
  • 8
  • 17