0

I have an OptionMenu inside my activity but when I choose an option it shows nothing. I've found some tutorials but they show what I already do. What is wrong? Thank you for the replies.

This is the code at the moment:

public class Listino extends TabActivity { final Context context = this; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); deleteFile("Ordinazioni.txt"); setContentView(R.layout.show_listino); TabHost tabHost = getTabHost(); //Primi Intent intentPrimi = new Intent().setClass(this, Primi.class); TabSpec tabSpecPrimi = tabHost .newTabSpec("Primi") .setIndicator("Primi") .setContent(intentPrimi); //Secondi Intent intentSecondi = new Intent().setClass(this, Secondi.class); TabSpec tabSpecSecondi = tabHost .newTabSpec("Secondi") .setIndicator("Secondi") .setContent(intentSecondi); // Dolci Intent intentDolci = new Intent().setClass(this, Dolci.class); TabSpec tabSpecDolci = tabHost .newTabSpec("Dolci") .setIndicator("Dolci") .setContent(intentDolci); // Pizze Intent intentPizze = new Intent().setClass(this, Pizze.class); TabSpec tabSpecPizze = tabHost .newTabSpec("Pizze") .setIndicator("Pizze") .setContent(intentPizze); // Bevande Intent intentBevande = new Intent().setClass(this, Bevande.class); TabSpec tabSpecBevande = tabHost .newTabSpec("Bevande") .setIndicator("Bevande") .setContent(intentBevande); // Contorni Intent intentContorni = new Intent().setClass(this, Bevande.class); TabSpec tabSpecContorni = tabHost .newTabSpec("Contorni") .setIndicator("Contorni") .setContent(intentContorni); tabHost.addTab(tabSpecPrimi); tabHost.addTab(tabSpecSecondi); tabHost.addTab(tabSpecPizze); tabHost.addTab(tabSpecDolci); tabHost.addTab(tabSpecBevande); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.menu.opzioni_menu, menu); return true; } public boolean OnOptionsItemSelected(MenuItem item) { Toast.makeText(context, item.getItemId(), Toast.LENGTH_SHORT).show(); switch (item.getItemId()) { case R.id.fineordinazione: Intent intent = new Intent(context, AggiungiProdotto.class); startActivity(intent); return true; case R.id.modificaordinazione: break; } return false; } 

}

Aaaw.

4
  • Naming variables in your native language (!= en) makes me giggle every time. Commented Mar 5, 2013 at 15:24
  • Like this: youtube.com/watch?v=w-s8MlzzN9U ? :) Commented Mar 5, 2013 at 15:25
  • you forgot to @Override the OnOptionsItemSelected Commented Mar 5, 2013 at 15:29
  • I don't have to. Eclipse marks it as error. Commented Mar 5, 2013 at 15:42

3 Answers 3

1

There is a typo.

Not

public boolean OnOptionsItemSelected(MenuItem item) 

but

public boolean onOptionsItemSelected(MenuItem item) 

The initial letter of the method name is a lower case.

Sign up to request clarification or add additional context in comments.

1 Comment

"Sometimes I feel like throwing my hands up in the air. I know I can count on you." -cit. Thank you, 2 hours of debugging now has sense.
0

You are supposed to inflate menus through getMenuInflater(), not layouts.

Change:

menuInflater.inflate(R.layout.opzioni_menu, menu); 

To this:

menuInflater.inflate(R.menu.opzioni_menu, menu); //use menu, not layout 

You should use R.menu.bla_bla_bla instead of R.layout.bla_bla_bla.

For more info, read this.

6 Comments

I don't have any menu.opzioni_menu
then create one, otherwise you cannot inject menus to your activity.
Changed, but doesn't appear nothing as before. I click on my option and it doesnt change ativity. I see the app doesnt enter in the switch.
clean + refresh your project and then build again
Still nothing. I'm frustrated :S
|
0

create opzioni_menu.xml in res/menu/ (if you don't have menu folder, create one)

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/fineordinazione" android:title="fineordinazione"/> <item android:id="@+id/modificaordinazione" android:title="modificaordinazione"/> </menu> 

then change

 menuInflater.inflate(R.layout.opzioni_menu, menu); 

to

 menuInflater.inflate(R.menu.opzioni_menu, menu); 

Good luck hope this help !!

edit

public boolean OnOptionsItemSelected(MenuItem item) 

to

@Override public boolean onOptionsItemSelected(MenuItem item) 

On <<<< o in lower case !! please

6 Comments

I've done like this but when I press the button nothing appears, it doesnt enter in the switch
remove Toast.makeText(context, item.getItemId(), Toast.LENGTH_SHORT).show(); or change to Toast.makeText(context, String.valueOf(item.getItemId()), Toast.LENGTH_SHORT).show(); if you really want to see Id
The problem is the app doesnt reach that point, seems it doesnt enter in OnOptionsItemSelected
just try , may be it's stuck in Toast line
Nothing, I can't find the problem :(
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.