Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top