Take the 2-minute tour ×

I've come across a problem that I have searched and searched for a solution for, and cannot find one. I am trying to set up a Preference screen which is opened by an action button on an ActionBarSherlock action bar. The buttons appear on the action bar, but they don't do anything when pressed. They are supposed to start a new intent, but it doesn't. Here's some code:


package com.myname.minteract;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import android.content.Intent;
import android.os.Bundle;

public class Main extends SherlockFragmentActivity {

    public void onCreate(Bundle savedInstanceState) {

    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.activity_main, menu);
        menu.add("Add Server")
        menu.add("Refresh Servers")

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.addServer:
            startActivity(new Intent(this, AddServer.class));
        case R.id.settings:
            startActivity(new Intent(this, Settings.class));

My other class files and xml files are identical to the ones found here, with minor name changes (posting all of my code would take up an awful lot of space).

Hopefully this is enough information. I am fairly new to Android and very new to ActionBarSherlock, so I may be missing something fairly easy. I've tried using logcat and such, but the button literally does nothing, it doesn't even show up on the logcat with an error.


Extra note: I am testing my app using a HTC Sensation 4G running ICS and a MyTouch 3G running Froyo (to make sure it is compatible with pre-honeycomb devices).


Here's my /res/menu/activity_main.xml:

    <item android:id="@+id/menu_settings"
        android:showAsAction="never" />
    <item android:id="@+id/addServer"
        android:title="Add Server" />
    <item android:id="@+id/refreshServers"
        android:title= "Refresh Servers" />
    <item android:id="@+id/settings"
        android:title="Settings" />
share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

You've set the ID of everything in the XML file, and set everything else up there as well, so there is no need to use menu.add(...) in your main code. Simply inflate the Menu and all will be well.

Additionally, since you've handled the event, return true instead. Try this:

public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.activity_main, menu);

Personally I would recommend adding these as <item ...>s in res/menu/activity_main.xml instead of programmatically, but if this works for you, then that's what matters, right?

share|improve this answer
.setId doesn't seem to work for me, it says "The method setId(int) is undefined for the type MenuItem". I will edit the original post because I think I did specify the Id in res/menu/activity/main.xml. –  Matthieu Aug 4 '12 at 4:15
Then you shouldn't have to call any menu.add(...)s at all. The items should all be added through the XML. If you remove these lines, and change the return type to true, does it work? –  Eric Aug 4 '12 at 4:36
Sweet, it works! I'm not sure why I was doing the same thing in two different places. I also needed to add android:showAsAction="always" to each item in my activity_main.xml. Thanks for the help. –  Matthieu Aug 4 '12 at 5:16
add comment

Your Answer


