Kaloyan Roussev
Kaloyan Roussev

Reputation: 14711

Toast. Can not chain methods, but also can not invoke it in a normal way?

Toast.makeText(net.asdqwe.activities.Signup.this, configurationz.ERROR_MESSAGES_SIGNUP_PASSWORDS_DO_NOT_MATCH, Toast.LENGTH_SHORT).setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL, 0, 0);

Toast.show();

This code doesnt work. Toast.show() is marked in red with the following error:

Cannot make a static reference to the non-static method show() from the type Toast

Toast.makeText(net.asdqwe.activities.Signup.this, 
        configurationz.ERROR_MESSAGES_SIGNUP_PASSWORDS_DO_NOT_MATCH,
        Toast.LENGTH_SHORT)
            .setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL, 0, 0)
            .show();

This also doesnt work, giving me the following error:

Cannot invoke show() on the primitive type void

Upvotes: 0

Views: 195

Answers (2)

Vipul
Vipul

Reputation: 28093

show() method is not a static method,So you cant call Toast.show();

Rather you should use following.

Toast toast=Toast.makeText(net.asdqwe.activities.Signup.this, configurationz.ERROR_MESSAGES_SIGNUP_PASSWORDS_DO_NOT_MATCH, Toast.LENGTH_SHORT);

toast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL, 0, 0);

toast.show();

Upvotes: 1

Vegard
Vegard

Reputation: 4497

Show is not a static method, so you need to use the instance you create with the makeText method. This is how you could do it:

Toast myToast = Toast.makeText(net.asdqwe.activities.Signup.this, configurationz.ERROR_MESSAGES_SIGNUP_PASSWORDS_DO_NOT_MATCH, Toast.LENGTH_SHORT);
myToast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL, 0, 0);
myToast.show();

Upvotes: 2

Related Questions