Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

menu not appearing in Fragment

I'm attempting to put a menu into a fragment in my app. However, the menu isn't appearing when I run it. My understanding of the steps involved in making a menu display in a fragment (and please correct me if I'm wrong or missing something) is that you do the following:

  • Create a menu resource file in the res/menu directory.
  • Override onCreateOptionsMenu(Menu, MenuInflater) and within said method, inflate the layout defined by the menu resource ID.
  • Notify the fragment manager that this fragment should receive a call to onCreateOptionsMenu by calling setHasOptionsMenu(true) in the fragment's onCreate method.

I've written a reduced version of my code to only include the bare minimum of what (I believe) should show a menu. Can anyone tell me what's missing from this code?

Here is my menu resource xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/menu_item_1"
        android:icon="@android:drawable/ic_menu_add"
        android:title="@string/menu_item_text"
        android:showAsAction="ifRoom|withText"/>
</menu>

My fragment code:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;

public class MainFragment extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.fragment_menu, menu);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_main, parent, false);
        return v;
    }
}

And my activity code:

package com.bignerdranch.android.fragmentmenuexample;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
        if (fragment == null) {
            fragment = new MainFragment();
            fm.beginTransaction()
                    .add(R.id.fragmentContainer, fragment)
                    .commit();
        }
    }
}

And my manifest:

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

I'm running this on an AVD Galaxy Nexus running IceCreamSandwich.

like image 768
rurouniwallace Avatar asked Mar 21 '15 17:03

rurouniwallace


1 Answers

Problem solved. My styles.xml file had the following:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

I added a new resource directory values-v14 and added the following style to it:

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
like image 150
rurouniwallace Avatar answered Sep 22 '22 11:09

rurouniwallace