3

My work code is

@Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) 
    {
        Toast tst;

        if (item.getItemId() == R.id.menuVada) {
            tst = Toast.makeText(Main.this, " Vada ", 2000);

        } else {
            tst = Toast.makeText(Main.this, "Menu Title: " + item.getTitle()
                    + " Menu ID: " + item.getItemId(), 2000);
        }
        tst.setGravity(Gravity.CENTER, 0, 0);
        tst.show();

        return true;

    }

but this code not work

@Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) 
    {
        Toast tst;

        if (item.getItemId() == R.id.menuVada) {
            //tst = Toast.makeText(Main.this, " Vada ", 2000);

        } else {
            tst = Toast.makeText(Main.this, "Menu Title: " + item.getTitle()
                    + " Menu ID: " + item.getItemId(), 2000);
        }
        tst.setGravity(Gravity.CENTER, 0, 0);
        tst.show();

        return true;

    }

why not work my code ?

Please help.

Best Regards

Chicharito
  • 1,450
  • 8
  • 31
  • 49

7 Answers7

7

What if (item.getItemId() == R.id.menuVada)?

simply initialize tst to null. And then check if tst!=null show the toast

@Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) 
    {
        Toast tst = null;

        if (item.getItemId() == R.id.menuVada) {
            //tst = Toast.makeText(Main.this, " Vada ", 2000);

        } else {
            tst = Toast.makeText(Main.this, "Menu Title: " + item.getTitle()
                    + " Menu ID: " + item.getItemId(), 2000);
        }
        if(tst!=null){
            tst.setGravity(Gravity.CENTER, 0, 0);
            tst.show();
        }

        return true;

    }
Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
  • This could be cleaned up a bit by moving the declaration, .setGravity and .show into the else clause. – Mike dg Sep 19 '11 at 15:22
1

Your variable Toast tst; should be initialized in both if and else conditions.

becasue if you find your if() condition true then the else part will never be called. and application flow will proceed to next and will cause NullPointerException

Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
1

Yes, in the if branch, tst is not initialiyed. But if you are showing the Toast in only one branch, I'd put the declarataion, gravity and show lines in that branch too.

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) 
{

    if (item.getItemId() == R.id.menuVada) {

    } else {
        Toast tst = Toast.makeText(Main.this, "Menu Title: " + item.getTitle()
                + " Menu ID: " + item.getItemId(), 2000);
        tst.setGravity(Gravity.CENTER, 0, 0);
        tst.show();
    }

    return true;

}

Otherwise take Serif's way.

vmatyi
  • 1,273
  • 10
  • 21
0

I think you should make it static Like

Static  tst;

Or make it to store tst in another variable then acces your class Toast like

Toast  t = new Toast();
t.tst = // your variable

Hope it will work

Kirtikumar A.
  • 4,140
  • 43
  • 43
0

You have to initialize a variable before using it. Try this:

Toast tst = null;

But you will get a null reference exception if the code doesn't take the else branch. So, in a way, you could still say that the code doesn't work... It just compiles.

Jordão
  • 55,340
  • 13
  • 112
  • 144
0

Because if your if() test is true, it'll go into the empty branch where the assignment of tst is commented out. tst is therefore uninitialized when you get to tst.setGravity().

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
-1

You could simply try :

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) 
{
    if (item.getItemId() == R.id.menuVada) {
        Toast.makeText(Main.this, " Vada ", 2000);

    } else {
        Toast.makeText(Main.this, "Menu Title: " + item.getTitle()
                + " Menu ID: " + item.getItemId(), 2000);
    }
    return true;
}

EDIT accordind to comment.

mthpvg
  • 3,789
  • 3
  • 26
  • 34
  • I edited my post, thx for correcting me. My point was just that you do not need do have a Toast tst. – mthpvg Sep 19 '11 at 14:47