sss
sss

Reputation: 717

Adding second button in Action Bar

I'm trying to add a second button to my action bar. I have one drawable that opens a new activity. I want to add a second on in onOptionsItemSelected. Here is my code.

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    switch (item.getItemId()) {
        case R.id.new_account:
            Intent intent = new Intent(getActivity(), AddAccountActivity.class);
            this.startActivity(intent);
            break;
        switch (item.getItemId()) {
            case R.id.action_settings:
                Intent myIntent = new Intent(getActivity(), SettingsActivity.class);
                this.startActivity(myIntent);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}

I get an error asking in the last closing bracket saying, "Missing return statement." I am new to android development so I'm guessing it is something simple. Thanks.

Edit: Here is my main.xml in my menu folder

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.myapplication2.app.MainActivity" >

<item android:id="@+id/new_account"
    android:icon="@drawable/ic_action_new"
    android:showAsAction="always"/>

<item android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:orderInCategory="100"
    android:showAsAction="always" />

I haven't changed any lines of the settingsActivity.class. I just created a new activity with the "settings" mode.

Upvotes: 0

Views: 176

Answers (2)

Zusee Weekin
Zusee Weekin

Reputation: 1358

You are getting "Missing return statement" because you don't have a return statement in switch-case. For ex:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){
        case R.id.settings:
            setSettings();
            return true;
        case R.id.second:
            setColor();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }

    }

Upvotes: 1

Amadas
Amadas

Reputation: 703

If you want add menuItem, you must add item in xml.
Project-res-menu has a xml files.
There is already has a 1 item.
Try add new Item and set attribute, I think you show a two menu.

Upvotes: 1

Related Questions