19

I need an example or a tutorial on how to add menu items with action bar sherlock

When I use the simple menu with the imports

import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; 

when I call

@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.settings_menu, menu); return true; } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.goToSettings: startActivity(new Intent(this, SetPreference.class)); return true; default: return super.onOptionsItemSelected(item); } } 

then I get the Cannot override the final method from SherlockActivity error.

1
  • Ctrl+Shift+O to fix all your imports. Make sure you are using the "android support jar" that comes with ActionBarSherlock and make sure that the ActionBarSherlock is added on a project library to your project. Commented Jan 13, 2013 at 17:28

3 Answers 3

49

You have to use Menu, MenuInflater and MenuItem classes from com.actionbarsherlock.view package:

import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuItem; @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getSupportMenuInflater(); inflater.inflate(R.menu.settings_menu, menu); return super.onCreateOptionsMenu(menu); } 

BTW, ActionBarSherlock contains a lot of samples.

Sign up to request clarification or add additional context in comments.

4 Comments

Just like to comment that if you find your home button click doesn't work when using ActionBarSherlock, @StenaviN's answer will solve your problem. Sure wish I saw this 3 hrs ago...
There is only an error in code, the return statement must be: return super.onCreateOptionsMenu(menu);
@Guido However, the implementation of super.onCreateOptionsMenu(menu); is just public boolean onCreateOptionsMenu(Menu menu) { return true; }, at least if the super is SherlockFragmentActivity.
@Guido, I have corrected return statement as suggested. However here is a quote from developer documentation: You must return true for the menu to be displayed; if you return false it will not be shown.
3

I used @StenaviN 's answer above but ran into problems with onContextItemSelected. This post solved it for me.

Basically, you just have to use

@Override public boolean onContextItemSelected(android.view.MenuItem item) { /* ... */ } 

instead of

@Override public boolean onContextItemSelected(MenuItem item) { /* ... */ } 

Comments

3

I used @Matt's answer above but ran into problems with onContextItemSelected.

Basically, you just have to use

@Override public boolean onContextItemSelected(com.actionbarsherlock.view.MenuItem item) { /* ... */ } 

instead of

@Override public boolean onContextItemSelected(android.view.MenuItem item) { /* ... */ } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.