How to access an Activites Menu programatically?

by Carl Whalley » Wed, 19 Aug 2009 03:31:12 GMT

Sponsored Links
 I'm writing unit tests in a class derived from
ActivityInstrumentationTestCase2. I know I can access anything defined
in the applications XML using findViewById(), but this application
defines its Menu programmatically. I'm trying to access that menu so I
can fire click() events to it in my tests the same way I'm doing with
Button Views etc.

To illustrate, this works fine and shows I can control/test various

public void testMyAppEdit() {
        MyAppActivity main = (MyAppActivity) getActivity();

        EditText val1Edit = (EditText) main.findViewById(;

        Button addButton = (Button) main.findViewById(;

        TextView resultText = (TextView) main.findViewById(;

        assertEquals("Result incorrect", resultText.getText(), "3");

But I need to do the same to fire Menu events:

public void testMyAppAboutBox() {
        MyAppActivity main = (MyAppActivity) getActivity();

        // Huh? this menu has no id, and no obvious way of accessing it...
        //Menu menu = (Menu) main.findViewById(id);


Android Academy 

How to access an Activites Menu programatically?

by Carl Whalley » Wed, 19 Aug 2009 18:11:04 GMT

 hanks - just establishing theres no framework way to get menus.
So I did as you suggested and there still some wierdness. I added a
getter for the menu but it always returns null:

public class MyAppActivityTest extends
ActivityInstrumentationTestCase2< MyAppActivity> {

public void testMyAppAboutBox() {
MyAppActivity main = (MyAppActivity) getActivity();
Menu menu = main.getMenu();

Could it be the Activities onCreateOptionsMenu() hasn't been called?
Is there more I should be doing in my test to help establish the
Activities runtime context?

On a different thread (:-)), I tried this:

public void testAbout() {
MainActivity main = (MainActivity) getActivity();

Menu menu = main.getMenu();
menu.performIdentifierAction(, 0);

Hoping the key event would cause the menu to be inflated, but that
just causes:

java.lang.RuntimeException: This method can not be called from the
main application thread


On Aug 18, 8:59pm, Dianne Hackborn <> wrote:

Sponsored Links

Other Threads

1. Need help in display message which can display telephone number as a hyperlink

Hi All,
I am pretty new in Android.
I am facing one problem in message view.
Bacically what I need to do is in message view I want to mark any
telephone number or web address in different color. Can anyone please
tell me where exactly the text is been written in Message View?
On clicking this items it should launch appropriate event.
If anyone touches a telephone number it should launch the dialer or if
it is a valid email address then it should launch the Compose message
event and so on.

Another question is whether to display message is Android uses the

Looking forward to your help.
Thanking you.

Tarun S


2. Unparsed aapt error

Hi all,

I found some old posts on this problem, and I was having a similar issue, so
I thought I'd share what I learned.

I was getting this error after modifying an XML file, and I was unable to
start a debug session until I fixed the problem.  It seems if there are
errors that cause aapt to fail, eclipse can get in a state where it doesn't
know when the problem gets fixed.  Deleting did not work for me.
 What I ended up doing after I fixed the XML file is right-clicking on the
error message in the Problems view, and deleting the error.  Then modify
some files to force a rebuild and you should be okay.

I found it helps to turn the android log level to Verbose (Preferences ->
Android -> Build).  This led me to know that once I fixed the XML file, aapt
was happy, but eclipse was not.

Hopefully this will save someone else the 3 hours I burned trying to figure
it out!



3. how to start a "stop" activity

4. HTTP Post

5. I don't understand the layout weight. Why does my layout look so strange?

6. Exception while connecting to unternet

7. lower-level audio / streaming API?