John Jared
John Jared

Reputation: 800

Android Sherlock ActionBar OnCreateOption

I'm trying to add a next button in my app action bar ( Sherlock ), I'm new and couldn't find tut's anywhere so I tried using guides from Android.com, I want to add a next button into my first activity action bar

this is my Code at the StartActivity.java

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

and this is the xml of main.xml

  <menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/bNext"
          android:title="Next"
          android:showAsAction="ifRoom|withText" />
    </menu>

I get an error from R."menu".StartActivity

menu cannot be resolved or is not a field

Upvotes: 0

Views: 2291

Answers (2)

Android2390
Android2390

Reputation: 1150

First go into your menu folder and and add an .xml file which looks like this :

  <?xml version="1.0" encoding="utf-8"?>
  <menu xmlns:android="http://schemas.android.com/apk/res/android" >

     <item 
          android:id="@+id/share"
          android:title="Share"
          android:showAsAction="always"
          android:icon="@drawable/actionbar_share" />

   </menu>

Then you go into your activity where you are calling the action bar and do this :

      @Override
  public boolean onCreateOptionsMenu(Menu menu) 
     {
          MenuInflater inflater = getSupportMenuInflater();
          inflater.inflate(R.menu.action_bar_menu, menu);
          return super.onCreateOptionsMenu(menu);
     }

where action_bar is the xml file you created in the first step Then after you get your button in your action bar you need to add the

         public boolean onOptionsItemSelected(MenuItem item) 

method and add whatever code you want there to be

Upvotes: 3

Sparky
Sparky

Reputation: 8477

You are getting the error "menu cannot be resolved or is not a field" because you are trying to load a resource named "R.menu.StartActivity", but your menu is saved in file "main.xml". You need to either rename the file to "StartActivity.xml", or change the resource id to "R.menu.main".

Upvotes: 1

Related Questions