user12585593
user12585593

Reputation:

Why I cannot display toast message outside onCreate?

I want to create simple app which will display Toast message,using my own function called Count_BMI (later to develop)


    Button submit ;
    EditText weight;
    EditText height;
    EditText BMI;
    int BMI_value;
 String text = "Hello Adam";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button submit = findViewById(R.id.btnSumit);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Count_BMI();

            }
        });
    }

    public void Count_BMI() {
    }
 Toast.makeText(getApplicationContext(),
               "This a toast message",
    Toast.LENGTH_LONG)
            .show();

}

Why I cant display Toast in my own function? This is the error: enter image description here

Upvotes: 0

Views: 153

Answers (2)

lolloCreator
lolloCreator

Reputation: 75

Need change:

public void Count_BMI() {
}
Toast.makeText(getApplicationContext(), "This a toast message", Toast.LENGTH_LONG) .show();

To:

public void Count_BMI() {
Toast.makeText(getApplicationContext(), "This a toast message", Toast.LENGTH_LONG) .show();
}

The problem is that you wrote the toast outside the brackets

Upvotes: 0

Ahmed Nezhi
Ahmed Nezhi

Reputation: 318

The toast is outside the void, it should be:

public void Count_BMI() {
 Toast.makeText(getApplicationContext(),"This a toast message",Toast.LENGTH_LONG).show();
}

Upvotes: 1

Related Questions