views:

51

answers:

1

Hi

I need to enable a MenuItem when a previous screen (Activity) returns.

I tried this code:

... ((MenuItem)findViewById(R.id.menu_how)).setEnabled(true); ...

but a null pointer exception is launched.

BTW, the menu_how is set to false in xml; and the code is part of onActivityResult(int requestCode, int resultCode, Intent data) call.

A: 

where are you calling this? (Sorry, didn't read carefully) I think you need to call it after the menu is inflated (usually in OnCreateOptionsMenu). To do this, you can set a variable to true when the other Activity returns, then do ((MenuItem)findViewById(R.id.menu_how)).setEnabled(mMyBooleanField) in OnCreateOptionsMenu after the call to inflater.inflate.

Edit: To accomplish this in code, it might look something like this:

At the top of the class (along with all the other class members):
Boolean mEnableMenuItem = false;

In OnCreateOptionsMenu:
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_main, menu); ((MenuItem)findViewById(R.id.menu_how)).setEnabled(mEnableMenuItem );

In OnActivityResult:
mEnableMenuItem = true;

QRohlf
Thanks for your answer but, what I'm trying to do is enabling my MenuItem according to some conditions. This conditions are obtained from another activity so, I've included the ((MenuItem)findViewById(R.id.menu_how)).setEnabled=true in the onActivityResult(int requestCode, int resultCode, Intent data) code. This is causing a null pointer exception.
mauricio
That's what the above answer accomplishes. The thing is, I don't think you can set it directly in onActivityResult, you need to indirectly set it using a variable. I'll update my answer to be a little more specific, with code implementation.
QRohlf