I have a special class that holds static info and makes calculations based on special circumstances. This is a special custom class that does not extend any part of the activity or android environment whatsoever.
Because this class never really gets instantiated, it's mostly referred to on a static
level. It's still important for me because I hold a number of static
enums
that are vital to the application's flow.
Here's the issue: Because the class doesn't extend the android activity life cycle, I'm having trouble referencing any string values from the Application's resource files. (I have strings stored in custom XML files as special resources)
Here's how everything looks:
CUSTOM CLASS FOR ENUM:
public class CreepIDs {
public static Context context = App.getContext();
public static enum CreepId {
ROBODEE(0, resourceString(R.creeps.robodee));
public final int id;
public final String name;
CreepId(int id, String name){
this.id = id;
this.name = name;
};
};
public static String resourceString(int id){
return context.getResources().getString(id);
}
}
APP CLASS EXTENDING APPLICATION:
public class App extends Application{
private static Context mContext;
@Override
public void onCreate() {
super.onCreate();
mContext = getApplicationContext();
}
public static Context getContext(){
return mContext;
}
}
I've stepped through the app so far using the debug feature and App.getContext()
is always returning a Context
with a value of null
. I can't figure out why. I need to be able to reference the R.creeps.robodee
from a class that doesn't extend any part of the android lifecycle.
I would pass the context through the constructor, but since CreepId
is a static enum
CreepIDs
doesn't actually get instantiated. I only reference it from the main activity.
What do you think is the best solution here?