0

I have referred to this blog tutorial. But, I cant figure out, where did they get "R.menu.main" in this coding?

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

and.. where did they get "R.id.action_settings" ?

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

I tried to change my coding by adding a little bit based on this blog tutorial. My apps still crash, it suddenly close.

Adda
  • 81
  • 1
  • 8
  • 1
    Those methods have nothing to do with the dual `NavigationView`s, and were likely leftover from a basic project template, so you can just delete them. If you still have a crash, then you'll need to look at [the stack trace](http://stackoverflow.com/questions/23353173) to determine the cause. – Mike M. Aug 15 '18 at 15:14
  • Thank you sir. I already figure it out. Error at my toolbar. – Adda Aug 15 '18 at 15:23

2 Answers2

0

Without more information it sounds like you may be missing a menu.xml resource file. Try adding one to your project.

Android Development - getMenuInflater(R.menu.main, menu)

  • yes sir, i didnt put the this coding getMenuInflater(R.menu.main, menu. because I cant figure out where did it get the menu.main... (?) – Adda Aug 15 '18 at 15:33
0

What happens is that it asks you for an xml file where the menu elements are supposed to be, what you should do is create a folder called "menu" in your "res" directory as seen in the image and then in "menu" "create the resource "main.xml".

image

already in your "main.xml" file, you should find the id of the elements...

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
   <item
      android:id="@+id/action_settings"
      android:title="@string/home_item" />
   <item
      android:id="@+id/about_option"
      android:title="@string/option_item" />
   <item
      android:id="@+id/suggestions_nav"
      android:title="@string/suggestions" />
</menu>

I hope I've helped! :D

  • I already have the menu and all the item where I name it as activity_home_drawer and activity_home_drawer2. Should I make main.xml in the menu folder? – Adda Aug 15 '18 at 15:39
  • If you tell me that your menu is called "activity_home_drawer" then what you would have to do is replace in the line of code as follows: getMenuInflater().inflate(R.menu.activity_home_drawer , menu); – DavidBarbaran Aug 15 '18 at 15:55