Doubt in Toast.makeText(this, ...)

Asked

Viewed 371 times

1

This is the same code as a previous doubt, but now my question is another.

In the Toast.makeTest(this, msg, Toast.LENGTH_SHORT); gives the following error when compiling, I believe that because of the context of this this:

error: non-static variable this cannot be referenced from a static context
error: no suitable method found for makeText(Validacao,String,int)
method Toast.makeText(Context,CharSequence,int) is not applicable
(argument mismatch; Validacao cannot be converted to Context)
method Toast.makeText(Context,int,int) is not applicable
(argument mismatch; Validacao cannot be converted to Context)*

My questions are: What would the code for using the Toast in the method static isValid()? Can’t I use the Toast in methods Static?

I’ve tried to put getApplicationContext(), thus:

 Toast toast = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT);

But it was a mistake. I’ve tried Validacao.java, but it also makes a mistake:

Toast toast = Toast.makeText(Validacao.java, msg, Toast.LENGTH_SHORT);

Below the code of the class Java validation. with a construction method Validation() and a static method isValid() using the Toast, that when compiled, generate the above mentioned errors:

package br.com.joao.coursera.calculadora;

import android.content.Context;
import android.widget.EditText;
import android.widget.Toast;

public class Validacao {
    public  Validacao() {  }

    public static boolean isValid(EditText et, String msg) {
            if (et.getText().toString().isEmpty()) {
                Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
                toast.show();
                return false;
            } else {
                return true;
            }
    }
}

1 answer

3


The mistake happens because this does not exist in static objects/methods, but there is another error in the code. Following explanation below.


To use the Toast.makeText it is necessary to pass the context as parameter. It turns out that this is a class reference and does not exist in static objects/methods.

When you use Toast.makeText(this, msg, Toast.LENGTH_SHORT); in a Activity the code will work smoothly, this because, different from its class Validacao, the class Activity extends the class Context.

Now that we know we need an object of the type Context, Let’s go to the next step.

When objects of the type EditText, ImageView etc. Are created, automatically added the context of the Activity in objects (see in the documentation), so all these objects carry the context where it was created. To capture it, just use the method object.getContext().

That is, instead of using this or getApplicationContext() in the method isValid, utilize et.getContext(), for example:

public class Validation {

    public static boolean isValid(EditText et, String msg) {
            if (et.getText().toString().isEmpty()) {
                Toast.makeText(et.getContext(), msg, Toast.LENGTH_SHORT)
                     .show();
            }

            return !et.getText().toString().isEmpty();
    }
}
  • Thank you very much, it worked!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.