-2

I have been trying to figure out why my boolean is not changing when I press the button, when I changed it manually it worked, but it doesn't do any thing. I have tried to follow tutorials to the word but they don't work. Can anybody point out where I am going wrong?

public boolean onOptionsItemSelected(MenuItem menu) 
{
    MenuItem freeze = (MenuItem)findViewById(R.id.freeze);  
    // Handle item selection 
    switch (menu.getItemId()) { 
        case R.id.freeze: 
            if (freze == false){
                freze = true;
            } else {
                freze = false;
            }
            return true; 
        case R.id.toggleVolCount: 
            if (toggleVol == true){
                toggleVol = false;
            } else {
                toggleVol = true;
            }
            return true; 
        default: return super.onOptionsItemSelected(menu); 
    } 

Thanks for all your help, when I tried the code that was suggested and it didn't work I went back and changed the menu. Previously I had made a button with an onClick to create the menu, when created the icon with code the code that I had previously written worked fine. Hope this helps someone other than me so I don't feel like so much of an idiot.}

jcw
  • 5,132
  • 7
  • 45
  • 54
  • 1
    You mean `freeze`, not `freze`. That'd give you a compilation error though, so I'm guessing it's a typo. – keyser Jun 18 '12 at 06:42
  • 1
    I think its typo error. freeze != freze – Om3ga Jun 18 '12 at 06:42
  • [Have a look at this](http://www.droidnova.com/how-to-create-an-option-menu,427.html) – Praveenkumar Jun 18 '12 at 06:43
  • 1
    Let's not jump to conclusions. In the above code, 'freze' may well be (and probably is) a boolean member of the containing class spelled differently from 'freeze' to avoid a collision with the local variable in the method (though i personally prefer to use the this-keyword in similar situations). – Kallja Jun 18 '12 at 07:00
  • @user1462846 Could you post your menu definition XML (or creation code if you are doing it that way). Have you double checked that you are using the correct ID's with your menu items? – Kallja Jun 18 '12 at 07:02
  • in java all types do follow pass-by-value. I think that is the reason you are facing this problem. – Zombie Jun 18 '12 at 07:11
  • @droids That is not the issue here. The switch statement is using the int primitive type, which indeed is passed by value. Additionally the Android platform makes certain guarantees about objects returned by the findViewById(int)-method. – Kallja Jun 18 '12 at 07:24
  • @Jarkko thanks for taking time to explain. Since the question isn't that clear I thought the boolean value freze is not getting changes made inside the switch to outside the switch statement and commented that. anyways it was helpful for me in understanding. – Zombie Jun 18 '12 at 07:35

1 Answers1

1

In res folder create one folder menu like drawable

Create new xml file optionmenu.xml in that folder.

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


</menu>

In onCreate method write this code....

setOptionMenu(R.menu.optionmenu);

and in overide method of Menu write this code.....

@Override
    public boolean onOptionsItemSelected(MenuItem menu) {
        switch (menu.getItemId()) {
        case R.id.menuitem:
            startActivity(new Intent(this, Prefs.class));
            break;

case R.id.menuitem1:
            startActivity(new Intent(this, Prefs1.class));
            break;
        default:
            break;
        }

        return true;
    }
Arvind Kanjariya
  • 2,089
  • 1
  • 18
  • 23