Getting resources from non-activity classes

北城以北 提交于 2019-12-25 04:21:41

问题


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?


回答1:


So it's not pretty but I found a solution. In onCreate for the Main activity (The only place this info is even getting used right now), I've put the following code:

CreepIDs.context = getApplicationContext();

Since context is static public anyways, I'm directly assigning a value to it when the app starts. I only need the context so I can call resources and it won't be used for anything else so I don't think this will be a problem for me at all.

In the end, using the special App class just didn't work for me.

Thanks for everyone's assistance.




回答2:


Try this:

public class App extends Application {

    private static App INSTANCE;

    @Override
    public void onCreate() {
        super.onCreate();
        INSTANCE = this;
    }

    public static App getInstance() {
        return INSTANCE;
    }

}

It works good for me.




回答3:


Based off of Andrey's answer:

public class App extends Application {

private static App INSTANCE;

@Override
public void onCreate() {
    super.onCreate();
    INSTANCE = this;
}

public static App getInstance() {
    return INSTANCE;
}

}

then, in CreepsID:

public class CreepIDs {

public static Context context = App.getInstance();

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);
}

}

You're calling a static method to get the context of App, thus giving your static class access to the resources of the Activity.




回答4:


This link fixed everything for me: How can I get a resource content from a static context?

I just had to realize that I should create a completely new class with nothing but the code in Cristians answer. Remember to modify the Manifest!

Good luck:)



来源:https://stackoverflow.com/questions/24760679/getting-resources-from-non-activity-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!