I looked here to try and solve my problem. It is similar but I use fragments so when I need context I usually need to call getActivity().
Basically I have App.java as described in the link above, I have
android:name=".App" inside my <application> tag
added to my AndroidManifest.xml. Now I have this class I use to collect all the things i use frequently:
public class MiscMethods{
public static void ErrorToast(int errorCode) {
String errorString = null;
if(errorCode==1){ errorString = App.getContext().getString(R.string.error_tooManyFieldsEmpty);}
if(errorCode==2){ errorString = App.getContext().getString(R.string.error_featureComingSoon);}
if(errorCode==3){ errorString = App.getContext().getString(R.string.error_SwitchBreak);}
else{errorString="Wrong Error Code";}
Toast errormsg = Toast.makeText(App.getContext(), errorString, Toast.LENGTH_SHORT);
errormsg.setGravity(Gravity.CENTER, 0, 0);
errormsg.show();
}
}
In one of my fragments I call
MiscMethods.ErrorToast(1);
I just get the "Wrong Error Code" message from the "else {}" part of my method
Can you help me make this right?