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;
}
}
}