Reputation: 2087
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:
onCreateOptionsMenu(Menu, MenuInflater)
and within said method, inflate the layout defined by the menu resource ID.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.
Upvotes: 5
Views: 5630
Reputation: 2087
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">
Upvotes: 4
Reputation: 1215
You could override the following function in your activity.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
This will solve the problem.
Upvotes: 0
Reputation: 11
U can simpley create a Android Sample Project, as guide you can choose with menu or othey style, you can find what different with your code.
Upvotes: 0
Reputation: 1566
I think you missed to override onCreateOptionsMenu
in your MainActivity
.
If you don't need to inflate any menu, simply return true
:
@Override
public boolean onCreateOptionsMenu ( Menu menu ) {
return true;
}
Upvotes: 0