Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7706927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:13:56+00:00 2026-06-01T00:13:56+00:00

This topic can look similar to others but I haven’t found any usable answer

  • 0

This topic can look similar to others but I haven’t found any usable answer for this case.

Here is what I want : I have a TabActivity with a menu, containing tabs without any menu. When I press the menu button, I want the only existing menu to be displayed. If I only inflate the menu, this is working fine. But if I want to change the content of the menu (change the visibility of items in the onPrepareOptionsMenu(menu) method) or even press the items of the menu, nothing works.

For the onPrepareOptionsMenu(menu), the problem seems to be coming from the menu.findItem(...) method which returns a null Object, and I have a NullPointerException !
In onOptionsItemSelected(item), none of the item IDs will be recognized.

I have noticed with the debugger that the menu Context is the activity of the current tab, so I have moved the menu inside of that activity instead, but without more success.

Last thing, I was using the same menu and very similar code in a previous version of the app, using a single activity (no tabs) and I didn’t encounter any problem. When I moved to the TabActivity design it was first working fine (maybe the context of the menu was my TabActivity instead of the Activity of the tab), but it didn’t work anymore after minor changes to the Activities of the tabs (nothing related to any menu).

If you think using ActionBar (and the support package) would fix this, I am already planning in moving to it later, but I would like to understand and fix this first.

Here is the code :

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu)
{
    MenuItem connect1 = menu.findItem(R.id.connect_1);
    MenuItem connect2 = menu.findItem(R.id.connect_2);

    // Here I do some stuff to prepare the menu, which could be simplified
    // like this :
    if (device1Connected)
        if (!connect1.isVisible()) // here I get the NullPointerException
            connect1.setVisible(true);
    return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.select_device:
        // do some stuff
        return true;
    case R.id.connect_1:
        // do some stuff
        return true;
    case R.id.connect_2:
        // do some stuff
        return true;
    case R.id.disconnect:
        // do some stuff
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

Here is the XML file of the menu, nothing special

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/select_device"
          android:icon="@drawable/icon"
          android:title="@string/choose_devices"/>
    <item android:id="@+id/connect_1"
          android:icon="@android:drawable/ic_menu_add"
          android:title="@string/connect_1" />
    <item android:id="@+id/connect_2"
          android:icon="@android:drawable/ic_menu_add"
          android:title="@string/connect_2" />
    <item android:id="@+id/disconnect"
          android:icon="@android:drawable/ic_menu_close_clear_cancel"
          android:title="@string/disconnect" />
</menu>

Edit : I have added the following in the onPrepareOptionsMenu(menu) to avoid the NullPointerException. It doesn’t fix the real problem but I can now see precisely what happens when I click on a MenuItem

if (connect1 == null || connect2 == null)
        return super.onPrepareOptionsMenu(menu);

When I click on the first item, getItemId() returns, for example, 2131165207, and the second item returns 2131165208 (I have checked, they are ids of Views of the second tab !!), but the values it should return to enter the switch/case are respectively 2131165215 and 2131165216, so as I said before, I have a problem with my item ids. I made this test with the menu within the Activity of the first tab, because the mContext value of the menu is always an instance of the current Activity. But even though the Context AND the Activity containing the menu are the same, it still doesn’t work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:13:58+00:00Added an answer on June 1, 2026 at 12:13 am

    So ! I have finally figured out what was wrong. It was a building/compiling error. I have been trying before refreshing the project many time, but it was not enough, I had also tried deleting the R.java file (where are compiling the ID numbers from the XML files), but it still didn’t work; and I finally remembered about the Project -> Clear... function in Eclipse while working on another project.

    And it just worked. So that was nothing there very challenging, but at least I’ve learnt something valuable today.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there is a lot on this topic but I can't get any
I've read the other posts here about this topic, but I can't seems to
This topic has been covered on this forums, but I haven't found a working
I saw some other similar questions on this topic here but they were not
I know this topic is much discussed but I can't seem to find any
There are similar questions out there on this topic, such as When can I
I've looked at other people's questions on this topic but can't seem to find
Please take a look at this topic : Can I calculate the average of
I am sure this topic has been discussed here before but I would like
I've read most of the relevant answers on this topic already, but I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.