Spoiler: this post may contain something stupid due to being used to C and new to Java programming
There is an activity MainActivity and a public non-activity class containing a number of methods. I need to show toast alerts for some of them
Current attempts are something like this, which fails with "Non-static method can not be referenced from a static context" for getApplicationContext():
void errorWarn (String warning) {
Context context = android.content.ContextWrapper.getApplicationContext();
Toast.makeText(context, "Something's wrong in " + warning, Toast.LENGTH_SHORT).show();
}
So, how to call toasts from a non-activity class?
UPD: The errorWarn is to be called from the methods in the class. So, if an error in a method of the class occurs, there should be an alert
We are in the MainActivity having an editText field. The class should get and parse the string from it. If on some step processing fails, it shows a toast in the MainActivity
UPD2: The full structure.
MainActivity:
public class MainActivity extends ActionBarActivity {
<...>
public void ButtonClick (View view) {
Class.testfunc("");
}
}
Class:
public class Class {
void errorWarn (Context context, String warning) {
Toast.makeText(context, "Something must be wrong. " + warning, Toast.LENGTH_SHORT).show();
}
void testfunc (String string) {
errorWarn(string);
}
}
Define your method so that it takes a Context
in argument and pass your Activity
to it.
in YourOtherClass
public class YourOtherClass {
public void showToast(Context context, String message){
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
}
or if you want to take a Context in your constructor and use showToast
only in YourOtherClass
:
public class YourOtherClass {
private Context context;
public YourOtherClass(Context context){
this.context = context;
}
private void showToast(String message){
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
}
in MainActivity
new YourOtherClass().showToast(this, message);
or if you have a Context
member variable of YourOtherClass
and you want to pass the Context
in the constructor of YourOtherClass
, you would do
new YourOtherClass(this).showToast(message);
// showToast doesn't have to take a Context as argument, it could just take one as constructor parameter and hold that.
// But then you have to make sure YourOtherClass is not used anymore if the Activity is closed.
And for the errors you got in the code your provided :
Context context = com.example.ex3.MainActivity;
This fails because you are trying to assign a type to an instance.
MainActivity.errorWarn("here");
This fails because you are calling a non-static method (the method has no static modifier in its signature) as if it was a static one. Have a look at this question for more details on static vs non-static methods.
Without knowing what YourOtherClass
does or how its lifecycle is linked to the Activity's, it's hard to say, but having to touch UI from a class that is not UI related and doesn't have any reference to Context
your can use, that feels odd. Taking a Context
as argument of the constructor of YourOtherClass
is probably what you need, but be wary of leaking Context
and Activity
lifecycle.
Pass context param
void errorWarn (Context context,String warning) {
Toast.makeText(context, "Something's in wrong " + warning, Toast.LENGTH_SHORT).show();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With