Search code examples
javaandroidandroid-navigationview

How to solve the app crashes in doing navigation view on both side in Android?


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.


Solution

  • 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