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 yourActivity
to it.or if you want to take a Context in your constructor and use
showToast
only inYourOtherClass
:or if you have a
Context
member variable ofYourOtherClass
and you want to pass theContext
in the constructor ofYourOtherClass
, you would doAnd for the errors you got in the code your provided :
This fails because you are trying to assign a type to an instance.
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 toContext
your can use, that feels odd. Taking aContext
as argument of the constructor ofYourOtherClass
is probably what you need, but be wary of leakingContext
andActivity
lifecycle.