0

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
JRad the Bad
  • 511
  • 5
  • 25

4 Answers4

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.

JRad the Bad
  • 511
  • 5
  • 25
0

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.

Andrii Mishchenko
  • 2,626
  • 21
  • 19
  • Sorry. This didn't fix it. I suspect the issue lies in the fact that I can't pull the application context from App because App doesn't ever get referenced from a class that's a part of the Android life cycle. – JRad the Bad Jul 15 '14 at 14:43
0

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.

krodmannix
  • 845
  • 10
  • 30
  • As I just explained to Andrey above, this doesn't work. Yes, I did make sure the change the method called to `App.getInstance()`. This did not fix the issue. Everything remained exactly the same. In theory, this is what I want and I believe my current solution does the exact same thing. The problem is, I don't believe `public static Context context = App.getContext();` ever even gets called because `CreepIDs` is never actually instantiated. However, calling `App.getContext().getResources()` still returns `null` (Even if I change things to the suggested format above) – JRad the Bad Jul 15 '14 at 15:00
  • Well, why are you instantiating CreepIDs? If you're not (and you don't have a constructor), make it a static class. – krodmannix Jul 15 '14 at 15:14
  • I'm not instantiating `CreepIDs`. I'm using it as a static class already. That's the problem. I've found a round-about solution anyways. But thanks for trying. – JRad the Bad Jul 15 '14 at 15:20
  • Glad you found your solution, but in the example code you posted it was not `static`. – krodmannix Jul 15 '14 at 16:00
0

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:)

Community
  • 1
  • 1
Fubbe99
  • 35
  • 5