findViewById for MenuItem returns null

Instead of

favButton = (MenuItem) this.findViewById(R.id.fav_button);  

in onCreateOptionsMenu after getMenuInflater().inflate(R.menu.activity_main, menu);

favButton = menu.findItem(R.id.fav_button);

Use menu.findItem() to get the menu. But this needs to be done after the menu is inflated.

Also, to answer your q in comment, you could use onPrepareOptionsMenu to set the state of your menu. If this menu is a one time updating, you could use onCreateOptionsMenu too, which is called only once.


but if someone really needs View and not MenuItem (for different manipulations, for example to start animation) you can still get it the next way:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.your_menu_xml_file, menu);
    ...
    new Handler().post(new Runnable() {
        @Override
        public void run() {
            view = findViewById(R.id.menu_refresh_button);
            // view.startAnimation(animation);
        }
    });
    return true;
}