Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Why my app has stopped working in emulator after adding a menu

I tried to add menu bar in weather app from this site. Everything worked fine, but when I added onCreateOptionsMenu in main activity and onOptionsItemSelected as noted.

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



@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.Zagreb:

            updateDisplay();
            return true;
        case R.id.Rijeka:

            return true;
        case R.id.Osijek:

            updateDisplay();
            return true;
        case R.id.Split:

            return true;

        case R.id.Dubrovnik:

            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

here is menu.xml

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

<item
    android:id="@+id/Grad"
    android:showAsAction="always"
    android:title="Gradovi"/>

        <item
            android:id="@+id/Zagreb"
            android:showAsAction="never"
            android:title="Zagreb"/>

        <item
            android:id="@+id/Osijek"
            android:showAsAction="never"
            android:title="Osijek"/>

        <item
            android:id="@+id/Rijeka"
            android:showAsAction="never"
            android:title="Rijeka"/>

        <item
            android:id="@+id/Split"
            android:showAsAction="never"
            android:title="Split"/>

        <item
            android:id="@+id/Dubrovnik"
            android:showAsAction="never"
            android:title="Dubrovnik"/>

</menu>

Rest of the code is same as app weatherApp in teacher notes.

Strange thing is that menu don't show rendered in Preview, why ?