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

1

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

No Toast.makeTest(this, msg, Toast.LENGTH_SHORT); gives the following error at compile time, I believe 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 be the code to use Toast in method static isValid() ? Can not I use Toast on static methods?

I have tried to put getApplicationContext() , like this:

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

But it made a mistake. I have tried to Validacao.java , but it also gives error:

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

Below the code for the Validation.java class with a Validation () constructor method and a static isValid () method using Toast , which 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;
            }
    }
}
    
asked by anonymous 21.07.2018 / 17:46

1 answer

3

The error happens because this does not exist in static objects / methods, but there is another error in the code. Here's an explanation below.

To use Toast.makeText you need to pass context as a parameter. It happens 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 an Activity the code will work without problems, this is because, unlike its class Validacao , class Activity extends class Context .

Now that we know that an object of type Context is needed, let's go to the next step.

When objects of type EditText , ImageView etc. Created, the activity context is automatically added to the objects ( see documentation ), so all of these objects carry with them the context from where it was created. To capture it, just use the object.getContext() method.

That is, instead of using this or getApplicationContext() in method isValid , use 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();
    }
}
    
21.07.2018 / 18:31