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;
}
}
}
Thank you very much, it worked!
– João Ricardo