0

Edit: I'm an idiot, move on...

The code and xml is below. I'm not getting any errors, but the text doesn't display. Anyone know why this doesn't work?

Code:

public boolean onPrepareMenuOptions ( Menu menu ) {
    EditText currency = (EditText) menu.findItem ( R.id.currency );
    currency.setText ( "test" );
    return true;
}

@Override
public boolean onCreateOptionsMenu ( Menu menu ) {
    getMenuInflater ().inflate ( R.menu.new_project, menu );
    return true;
}

XML:

<EditText
    android:id="@+id/currency"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/currency_summary" />
abc32112
  • 2,457
  • 9
  • 37
  • 53
  • 1
    have u called sethasoptionmenu(true) in onCreate? – Poutrathor Nov 06 '13 at 15:22
  • Does it work when called in the `onCreate()` or `onResume()` methods? – jlhonora Nov 06 '13 at 15:27
  • 1
    I'm not calling sethasoptionmenu, but that only seems to apply to fragments? Haven't gotten to those yet. Learning one thing at a time.. :) Putting it in onCreate gives: "The method setHasOptionsMenu(boolean) is undefined for the type NewProjectActivity". – abc32112 Nov 06 '13 at 15:39
  • have you tried to add `return super.onPrepareOptionsMenu(menu);` not `return true` ? – Noob Nov 06 '13 at 15:39
  • 1
    Couldn't add to onCreate, "undefined for the type ". – abc32112 Nov 06 '13 at 15:43

2 Answers2

1

I think the problem is that onPrepareMenuOptions is not being called. Here is what the documentation says:

On Android 2.3.x and lower, the system calls onPrepareOptionsMenu() each time the user opens the options menu (presses the Menu button).

On Android 3.0 and higher, the options menu is considered to always be open when menu items are presented in the action bar. When an event occurs and you want to perform a menu update, you must call invalidateOptionsMenu() to request that the system call onPrepareOptionsMenu().

You could solve this problem in two ways:

1) Method 1: Update the text at the time of onCreateMenuOptions (can update the XML file directly if you want)

2) Method 2: Call invalidateOptionsMenu() somewhere in code, just before the menu is shown.

Update: I noticed a bug in your code, should be calling super.onPrepareMenuOptions()

public boolean onPrepareMenuOptions ( Menu menu ) {
    super.onPrepareOptionsMenu(menu); // this is important
    EditText currency = (EditText) menu.findItem ( R.id.currency );
    currency.setText ( "test" );
    return true;
}

@Override
public boolean onCreateOptionsMenu ( Menu menu ) {
    getMenuInflater ().inflate ( R.menu.new_project, menu );
    return true;
}    `
Amulya Khare
  • 7,718
  • 2
  • 23
  • 38
  • You're right! I thought it was called automatically, for some reason. – abc32112 Nov 06 '13 at 15:46
  • Adding a call to "onPrepareMenuOptions" solves the problem. However for some reason I get a NullPointerException on the line "currency.setText ( "test" );". Eclipse doesn't complain. – abc32112 Nov 06 '13 at 16:03
  • what do you mean by adding a call? Can you update the code or maybe post a new question.. not sure which one is considered better..? – Amulya Khare Nov 06 '13 at 16:09
0

try this...

 EditText currency = (EditText) menu.findItem ( R.id.currency );
 SharedPreferences fill = getSharedPreferences("PREFS", 0);
 currency.setText ( fill.getString("string_name","String_Value" );
Naveed Ali
  • 2,609
  • 1
  • 22
  • 37
  • I solved the problem above using method 1 pointed out by Amulya Khare. However, now I'm getting a NullPointerException thrown by "currency.setText" instead. – abc32112 Nov 06 '13 at 16:04
  • http://stackoverflow.com/questions/13179589/change-options-menu-during-runtime-invalidateoptionsmenu, follow this link it may help you... – Naveed Ali Nov 06 '13 at 16:23