0

I have a menu item system implemented into my action bar, I am trying to get a menu item to become invisible once clicked and then to become visible again after clicking a different menu item.

The reason for this is to prevent a menu item getting clicked more than once.

I have done the following attempt below however, I get a null pointer exception error. I believe I may need to implement some if statements here but I'm not sure how to go about it.

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.tasklistmenu,menu);

    return true;
}//OnCreateOptionsMenu


@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

        case R.id.allTasks:

            item.setVisible(false);


            MenuItem notStartedStatus = findViewById(R.id.notStartedStatus);
            notStartedStatus.setVisible(true);

            MenuItem inProgressStatus = findViewById(R.id.inProgressStatus);
            inProgressStatus.setVisible(true);

            MenuItem completeStatus = findViewById(R.id.completeStatus);
            completeStatus.setVisible(true);

Log error

Attempt to invoke interface method 'android.view.MenuItem 
android.view.MenuItem.setVisible(boolean)' on a null object reference

NEW CODE

 @Override
public boolean onPrepareOptionsMenu(Menu menu) {

    MenuItem allTasks = menu.findItem(R.id.allTasks);
    MenuItem notStartedStatus = menu.findItem(R.id.notStartedStatus);
    MenuItem completeStatus = menu.findItem(R.id.completeStatus);
    MenuItem inProgressStatus = menu.findItem(R.id.inProgressStatus);


    if(tasksIsVisible) {
        allTasks.setVisible(true);
    } else {
        allTasks.setVisible(false);
    }

    if(notStartedIsVisible) {
        notStartedStatus.setVisible(true);
    } else {
        notStartedStatus.setVisible(false);
    }

    if(completeIsVisible) {
        completeStatus.setVisible(true);
    } else {
        completeStatus.setVisible(false);
    }

    if(inProgressIsVisible) {
        inProgressStatus.setVisible(true);
    } else {
        inProgressStatus.setVisible(false);
    }

    return true;

}//OnPrepareOptions

 @Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

        case R.id.allTasks:

            tasksIsVisible = false;
            invalidateOptionsMenu();

1 Answer 1

1

Its not enough to change the isVisible variable. You have to call the setVisible() method every time you want to change the visibility. That method does more than just setting a boolean value, so just changing a boolean value will not do.

After changing the isVisible value to false, you need to call invalidateOptionsMenu() which will re-launch the menu by calling onPrepareOptionsMenu() again.

public boolean onPrepareOptionsMenu(Menu menu)
{
    MenuItem notStartedStatus = menu.findItem(R.id.notStartedStatus);      
    if(isVisible) 
    {           
        notStartedStatus.setVisible(true);
    }
    else
    {
        notStartedStatus.setVisible(false);
    }
    return true;
}

Try this code for making the menu items unvisible:

...
isVisible = false;
invalidateOptionsMenu();
...
10
  • I'm not too sure if I can completely understand your answer. I have updated my question with the new code, however it is not doing what I expected so you can see what I am doing wrong.
    – Alex
    Commented May 26, 2019 at 16:32
  • you should change the boolean value and then call invalidateOptionsMenu(); Commented May 26, 2019 at 16:36
  • I'm not too sure where you mean for me to do this?
    – Alex
    Commented May 26, 2019 at 16:40
  • when you want to trigger action hide/unhide , ex : onOptionItemSelected Commented May 26, 2019 at 16:46
  • 1
    You should create them outside of your onpreparemenu , they should be local Commented May 26, 2019 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.