I try to enable / disable the refresh button when something happens in my application, but I get an exception from the null pointer that I cannot understand. I set boolean addingRefresh or removingRefresh to true depending on the situation and then call invalidateOptionsMenu() to enable or disable the button, however the menu item returns null. I searched on the Internet why it could be, but canβt find anything.
Code for onCreateOptionsMenu() (called when invalidateOptionsMenu () is called)
@Override public boolean onCreateOptionsMenu(Menu menu) { if (addingRefresh) { //below line as well as other similar line cause exceptions menu.findItem(R.id.action_refresh).setEnabled(true); addingRefresh = false; } else if (removingRefresh) { menu.findItem(R.id.action_refresh).setEnabled(false); removingRefresh = false; } else if (addingLoading) { } MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main_activity_actions, menu); return super.onCreateOptionsMenu(menu); }
Thanks for any help!
source share