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

  • Home
  • SEARCH
  • 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 8379719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:15:59+00:00 2026-06-09T16:15:59+00:00

Here is my Code: @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater();

  • 0

Here is my Code:

  @Override
        public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.menu_search)
                .getActionView();
        searchView.setSearchableInfo(searchManager
                .getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(true);
        searchView.setSubmitButtonEnabled(true);
        return true;
    }

    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        MenuItem Login = menu.findItem(R.id.Login);
        MenuItem Logout = menu.findItem(R.id.Logout);
        MenuItem About = menu.findItem(R.id.About);
        MenuItem Profile = menu.findItem(R.id.Profile);

        // set the menu options depending on login status
        if (LoggedStatus == true) {
            // show the log out option
            Logout.setVisible(true);
            Login.setVisible(false);

            Profile.setVisible(true);
            About.setVisible(true);
        } else {
            // show the log in option
            Logout.setVisible(false);
            Login.setVisible(true);

            Profile.setVisible(false); // hide
            About.setVisible(true);
        }

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

        case R.id.Login:
            Intent i = new Intent(this, Login.class);
            startActivity(i);

            return true;
        case R.id.Logout:

            return true;
        case R.id.Profile:

            return true;
        case R.id.About:

            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

I am getting Null pointer exception on each of the four MenuItem’s.
I simply want one menu for login, and one for logout.

EDIT: This is an app utilizing the Action Bar, Overflow menu and Android 4.0+
Also, More Code. Here is menu.xml:

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

    <item
        android:id="@+id/menu_search"
        android:actionViewClass="android.widget.SearchView"
        android:icon="@android:drawable/ic_menu_search"
        android:showAsAction="always"
        android:title="search"/>
    <item
        android:id="@+id/Login"
        android:title="Login"/>
    <item
        android:id="@+id/Logout"
        android:title="Logout"/>
    <item
        android:id="@+id/Profile"
        android:title="My Profile"/>
    <item
        android:id="@+id/About"
        android:title="About"/>

</menu>
  • 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-09T16:16:01+00:00Added an answer on June 9, 2026 at 4:16 pm

    You can set visibility of MenuItem in onPrepareOptionsMenu. Try to move there.

    About bonus: . In DOC they told onPrepareOptionsMenu is called everytime you press menu . I tested with one button change boolean param onClick to show/hide MenuItem and everything’s fine

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

Sidebar

Related Questions

Here in this code I have tried some touch screen event @Override public boolean
Here is the relevant code: @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) {
I'm getting a NullPointerException on my Dialog. Here's my code: @Override public boolean onDoubleTap(MotionEvent
Here is the relevent code: ... Resources res; @Override public void onCreate(Bundle savedInstanceState) {
Here is my code: new Loading.LoadTast(ctx) { @Override protected String doInBackground(Integer... params) { Looper.prepare();
Here a code to demonstrate an annoying problem: class A { public: A(): m_b(1),
Here is code example class A{ int i; public: A(int i) : i(i) {}
Here is the code: public class beautiful extends Activity { ImageView radar = (ImageView)
Here is my code: public class TasksList extends ListActivity { ... private static final
I'm working on a piece of code that could switch image using Menu inflater,

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.