Can't override onCreateOptionsMenu?

7,617 views
Skip to first unread message

Matt Kranzler

unread,
Jul 6, 2011, 2:21:05 PM7/6/11
to jakewharton-projects
I am trying to use this project in an app and when I try to override
onCreateOptionsMenu I can't since it is final in FragmentActivity. I
run into the same problem trying to compile the Feature Demo sample.
Any suggestions or am I missing something?

Jake Wharton

unread,
Jul 6, 2011, 2:31:46 PM7/6/11
to jakewharto...@googlegroups.com
Change your Menu import from android.view.Menu to android.support.v4.view.Menu and the override will work.

The method is marked as final in FragmentActivity to prevent people from overriding the incorrect version.

Matt Kranzler

unread,
Jul 6, 2011, 2:39:45 PM7/6/11
to jakewharton-projects
I am importing the support Menu and it is still saying the same thing.
I downloaded the latest source for the Feature Demo and it has the
same problem. Also, I tried overriding the onCreateOptionsMenu() in
Fragment and while this works, the items didn't show up in the menu or
on the ActionBar. Should this also work or is this not supported with
this project?

On Jul 6, 1:31 pm, Jake Wharton <jakewhar...@gmail.com> wrote:
> Change your Menu import from android.view.Menu to
> android.support.v4.view.Menu and the override will work.
>
> The method is marked as final in FragmentActivity to prevent people from
> overriding the incorrect version.
>
> ---
> Jake Whartonhttp://about.me/jakewharton

Matt Kranzler

unread,
Jul 6, 2011, 5:24:29 PM7/6/11
to jakewharton-projects
Ok, I just removed the final from the onCreateOptionsMenu() method
(both were final in FragmentActivity for some reason). This gets me
going however I am now getting a ClassCastException when trying to
inflate the menu. Here is the stack trace:

java.lang.ClassCastException:
com.android.internal.view.menu.MenuBuilder
at
android.support.v4.view.MenuInflater.inflate(MenuInflater.java:88)
at
com.tvshowfavs.android.ui.phone.SeriesInfoActivity.onCreateOptionsMenu(SeriesInfoActivity.java:
33)
at android.app.Activity.onCreatePanelMenu(Activity.java:2158)
at
com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:
328)
at
com.android.internal.policy.impl.PhoneWindow.onKeyDownPanel(PhoneWindow.java:
573)
at
com.android.internal.policy.impl.PhoneWindow.onKeyDown(PhoneWindow.java:
1215)
at com.android.internal.policy.impl.PhoneWindow
$DecorView.dispatchKeyEvent(PhoneWindow.java:1691)
at
android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:
2561)
at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:
2536)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1868)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3835)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit
$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
599)
at dalvik.system.NativeStart.main(Native Method)

The menu being passed in is a
com.android.internal.view.menu.MenuBuilder menu rather than a
com.actionbarsherlock.internal.view.menu.MenuBuilder menu. Any
suggestions?

On Jul 6, 1:31 pm, Jake Wharton <jakewhar...@gmail.com> wrote:
> Change your Menu import from android.view.Menu to
> android.support.v4.view.Menu and the override will work.
>
> The method is marked as final in FragmentActivity to prevent people from
> overriding the incorrect version.
>
> ---
> Jake Whartonhttp://about.me/jakewharton

Jake Wharton

unread,
Jul 6, 2011, 6:10:27 PM7/6/11
to jakewharto...@googlegroups.com
Make sure your items are set to display in the action bar. The default will not include them! (they're pushed to the normal options menu)  Either set android:showAsAction in the XML with ifRoom or always, or, if you are adding them manually, you can call setShowAsAction on the support version of MenuItem and pass in the appropriate constant(s) (http://developer.android.com/reference/android/view/MenuItem.html).

As for the override problem make sure your project is set to use JDK 1.6 (Java 6) otherwise the overrides won't work at all. You should not need to modify the library at all. That method is final for a reason (it's unused, sort of. more below). The library calls the method which uses the support version so that it can be called with the special versions of the menu-related classes which contain the features not natively present.

The feature demo and all of the samples are verified to work during every release process automatically so I'm quite positive they are set up properly. If you are having errors right out of the gate it usually indicates and error with the project setup. Double check all of the following:
  • The projects of the library and the sample are using either Android 3.0 or 3.1.
  • The projects actually show an entry in the file list for the specified Android version. Sometimes the Android SDK does not get attached when importing an Android project. Recreating the project or sometimes even closing and then opening it will fix this.
  • Make sure the sample project references the library project and has it listed in its project. Closing and reopening the project will usually fix this.
  • Make sure you are targetting JDK 1.6 (Java 6) on all of the projects.
  • Use android.support.v4.* classes instead of android.* classes in most cases. A good way to check this is to delete the imports related to menu and go through the errors one-by-one in Eclipse telling it to import the support classes.
Beyond these things it's hard for me to diagnose exactly what the problem is without seeing real code. If none of the above work we can discuss a way for me to either obtain the code or watch your desktop in a manner that's comfortable with you.

Matt Kranzler

unread,
Jul 6, 2011, 8:04:23 PM7/6/11
to jakewharton-projects
Thanks for the support Jake. I finally found out why I was getting the
exception. Now the only problem I am having is that when I try to
inflate from an xml file the items won't show up in the ActionBar. I
have them set to "showAlways" and it still doesn't show. Also, it does
show up in my options menu but it doesn't show the text I have set,
just the icon. If I add the item programmatically the item will show
up in the ActionBar. Any suggestions?


On Jul 6, 5:10 pm, Jake Wharton <jakewhar...@gmail.com> wrote:
> Make sure your items are set to display in the action bar. The default will
> not include them! (they're pushed to the normal options menu)  Either set
> android:showAsAction in the XML with ifRoom or always, or, if you are adding
> them manually, you can call setShowAsAction on the support version of
> MenuItem and pass in the appropriate constant(s) (http://developer.android.com/reference/android/view/MenuItem.html).
>
> As for the override problem make sure your project is set to use JDK 1.6
> (Java 6) otherwise the overrides won't work at all. You should not need to
> modify the library at all. That method is final for a reason (it's unused,
> sort of. more below). The library calls the method which uses the support
> version so that it can be called with the special versions of the
> menu-related classes which contain the features not natively present.
>
> The feature demo and all of the samples are verified to work during every
> release process automatically so I'm quite positive they are set up
> properly. If you are having errors right out of the gate it usually
> indicates and error with the project setup. Double check all of the
> following:
>
>    - The projects of the library and the sample are using either Android 3.0
>    or 3.1.
>    - The projects actually show an entry in the file list for the specified
>    Android version. Sometimes the Android SDK does not get attached when
>    importing an Android project. Recreating the project or sometimes even
>    closing and then opening it will fix this.
>    - Make sure the sample project references the library project and has it
>    listed in its project. Closing and reopening the project will usually fix
>    this.
>    - Make sure you are targetting JDK 1.6 (Java 6) on all of the projects.
>    - Use android.support.v4.* classes instead of android.* classes in most

Jake Wharton

unread,
Jul 6, 2011, 8:20:11 PM7/6/11
to jakewharto...@googlegroups.com

How are you obtaining the menu inflater instance? You should be using getMenuInflater() rather than calling getSystemService(Context.MENU_INFLATER) so that you're getting the custom inflater.

Matt Kranzler

unread,
Jul 6, 2011, 8:25:25 PM7/6/11
to jakewharton-projects
I am using the getMenuInflater method from FragmentActivity.

On Jul 6, 7:20 pm, Jake Wharton <jakewhar...@gmail.com> wrote:
> How are you obtaining the menu inflater instance? You should be using
> getMenuInflater() rather than calling
> getSystemService(Context.MENU_INFLATER) so that you're getting the custom
> inflater.

Jake Wharton

unread,
Jul 6, 2011, 8:48:35 PM7/6/11
to jakewharto...@googlegroups.com

Action items must have icons specified in order to show up on pre-3.0 action bars. Might that be the issue?

---
Jake Wharton
http://about.me/jakewharton

On Jul 6, 2011 8:25 PM, "Matt Kranzler" <matt.w....@gmail.com> wrote:

Matt Kranzler

unread,
Jul 6, 2011, 8:56:34 PM7/6/11
to jakewharton-projects
Ok I figured out the problem. I was just hardcoding text for the
android:title attribute. When I use a string resource it works as
intended. Should it work with hardcoded strings for titles? (I don't
plan on hardcoding it, I was just in a hurry and didn't create string
resources for them)

On Jul 6, 7:48 pm, Jake Wharton <jakewhar...@gmail.com> wrote:
> Action items must have icons specified in order to show up on pre-3.0 action
> bars. Might that be the issue?
>
> ---
> Jake Whartonhttp://about.me/jakewharton
> On Jul 6, 2011 8:25 PM, "Matt Kranzler" <matt.w.kranz...@gmail.com> wrote:

Matt Kranzler

unread,
Jul 6, 2011, 8:59:38 PM7/6/11
to jakewharton-projects
Also, does the onCreateOptionsMenu in Fragment not work with the
support actionbar?

Jake Wharton

unread,
Jul 6, 2011, 9:04:03 PM7/6/11
to jakewharto...@googlegroups.com

Cool. I'll file a bug report on GitHub when I get home (unless you want to). It probably only checks for a resource rather than an actual string.

The Fragment onCreateOptionsMenu should work. I know other implementers have used it and I think there's a test case for it. I'll investigate when I get home in an hour or two.

---
Jake Wharton
http://about.me/jakewharton

Matt Kranzler

unread,
Jul 6, 2011, 9:26:56 PM7/6/11
to jakewharton-projects
Thanks again!

On Jul 6, 8:04 pm, Jake Wharton <jakewhar...@gmail.com> wrote:
> Cool. I'll file a bug report on GitHub when I get home (unless you want to).
> It probably only checks for a resource rather than an actual string.
>
> The Fragment onCreateOptionsMenu should work. I know other implementers have
> used it and I think there's a test case for it. I'll investigate when I get
> home in an hour or two.
>
> ---
> Jake Whartonhttp://about.me/jakewharton
> On Jul 6, 2011 8:56 PM, "Matt Kranzler" <matt.w.kranz...@gmail.com> wrote:> Ok I figured out the problem. I was just hardcoding text for the

Jake Wharton

unread,
Jul 6, 2011, 11:08:17 PM7/6/11
to jakewharto...@googlegroups.com
This has been fixed in the dev branch.

Matt Kranzler

unread,
Jul 7, 2011, 9:21:30 AM7/7/11
to jakewharton-projects
Any progress on the Fragment onCreateOptionsMenu()? I noticed that the
Menu parameter is the support menu, but the inflater is the regular
MenuInflater. Could this have anything to do with the issue?

Jake Wharton

unread,
Jul 7, 2011, 1:52:33 PM7/7/11
to jakewharto...@googlegroups.com
I forgot to ask if you were calling setHasOptionsMenu(boolean) in the fragment. This is required to get onCreateOptionsMenu to be called. You can read about it here: http://developer.android.com/guide/topics/fundamentals/fragments.html#ActionBar.

With the following code, I was able to get the shakespeare sample's DetailsFragment to display an action item.

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

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.add("Hi")
   .setIcon(android.R.drawable.btn_star)
   .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}

Matt Kranzler

unread,
Jul 7, 2011, 1:55:24 PM7/7/11
to jakewharton-projects
Wow, that was it. Boy do I feel dumb.... I have had that haunt me a
few times already with the regular support pack! I keep forgetting
about that! Thanks again and thanks for your hard work!

On Jul 7, 12:52 pm, Jake Wharton <jakewhar...@gmail.com> wrote:
> I forgot to ask if you were calling setHasOptionsMenu(boolean) in the
> fragment. This is required to get onCreateOptionsMenu to be called. You can
> read about it here:http://developer.android.com/guide/topics/fundamentals/fragments.html...
> .
Reply all
Reply to author
Forward
0 new messages