title: Android actionbar menu item
body: <p>I have a item menu (sorting list) in my actionbar that I have to set its visibility to VISIBLE / GONE (depending on the list size -> empty or not).</p>
  
  <p>So what I need is to check if listview is empty or not and set the visibility of that menu item accordingly, and I managed to came up with some code to do that (check listview size in onCreateOptionsMenu and set menu item visibility), but the problem is that the list can change its content both from same activity or from another, leaving me no option (IMO) than to check again listview size and set visiblity in onResume().</p>
  
  <p>Here comes next problem: setting visibility of that item in onResume(), will throw a NPE, as I don't yet have that MenuItem initialized (onCreateOptionsMenu is called after onResume).
  Any ideas on how to solve this situation would be appreciated.</p>
  
  <pre><code>@Override
  public boolean onCreateOptionsMenu(Menu menu) {
      super.onCreateOptionsMenu(menu);
  
      getSupportMenuInflater().inflate(R.menu.main, menu);
  
      /*
       * initialize sortMenuItem so it can be used for turning visibility
       * on/off in onResume()
       */
      sortMenuItem = menu.findItem(R.id.menu_sort);
  
      return true;
  }
  
  @Override
  public void onResume() {
      super.onResume();
  
      boolean isListEmpty = mInterviewsList.isEmpty();
  
      Log.d(LOG_TAG, "sortMenuItem is null? " + (sortMenuItem == null)); 
              // sortMenuItem is indeed null
  
      sortMenuItem.setVisible(isListEmpty);
      sortMenuItem.setEnabled(!isListEmpty);
      this.invalidateOptionsMenu();
  
  }
  </code></pre>