Gautham Pughazhendhi
Gautham Pughazhendhi

Reputation: 352

My toast does not work when the button is clicked and it force closes and restarts the main activity

    Button btn = (Button) findViewById(R.id.button2);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Double s1 = 0.0;
            s1 = Double.parseDouble(height.getText().toString());
            Double s2 = 0.0;
            s2 = Double.parseDouble(weight.getText().toString());
            if (height.getText().length() == 0 || weight.getText().length() == 0 || (height.getText().length() == 0 && weight.getText().length() == 0)) {
                Context context = getApplicationContext();
                CharSequence text = "Enter the missing values";
                int duration = Toast.LENGTH_LONG;
                final Toast tost = Toast.makeText(context, text, duration);

                tost.show();
            } else {
                Double bmi;
                bmi = s2 / (s1 * s1);
                it.putExtra("bmi_val", bmi);
                startActivity(it);
            }


        }
    });

Upvotes: 1

Views: 53

Answers (1)

Derek Fung
Derek Fung

Reputation: 8211

You cannot use Application Context for Toast, you have to use Activity

Upvotes: 1

Related Questions