5

I am trying to make a toast display some text given certain conditions inside an onClickListener. The app won´t run in the simulator, and I get the following error: "void cannot be converted to Toast"

I have searched around, and find several similar problems and solutions on this forum, but none of them applies completely to my problem. The others haven´t used the correct context in the statement, but I really mean that I do. (The name of the javafile (context) is: "Case1Activity") Can anyone help me with this? I have simplified the code a bit:

public void onClick(View view) {
            if (button1Pushed == false){
                count++;
                Toast toast = Toast.makeText(Case1Activity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show();
            }


        }
    });
user820913
  • 623
  • 4
  • 12
  • 23

4 Answers4

9

do it without assignment statement

Toast.makeText(Case1Activity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show();
Maksim Ostrovidov
  • 10,720
  • 8
  • 42
  • 57
4

apply it as.

Toast.makeText(Case1Activity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show();
SRB Bans
  • 3,096
  • 1
  • 10
  • 21
3

if you want to use assignment operator then you can use below code

Toast toast = Toast.makeText(context, text, duration);
toast.show();
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
2

Dear Friend check below before eating Toast,

Your Toast (Incompatible types error) :

 Toast toast = Toast.makeText(Case1Activity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show(); 

Normal Case (Standard Use):

Toast.makeText(Case1Activity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show();`

Special Case (if you need reference of Toast):

 View toast = Toast.makeText(MainActivity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show();

because here ".show()" method is -

public void show () which shows the view for the specified duration.

Thanks

Androider
  • 3,833
  • 2
  • 14
  • 24