从非活动类获取资源 [英] Getting resources from non-activity classes

查看:223
本文介绍了从非活动类获取资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个特殊的类保存静态信息,并根据特殊情况进行计算。这是一个特殊的自定义类,不会扩展活动的任何部分或Android环境。



因为这个类从来没有获得实例化,它主要参考 static 级别。这对我来说仍然很重要,因为我持有一些对应用程序流程至关重要的 static $ code>枚举。 >

以下是问题:
由于该类没有扩展android活动生命周期,因此无法从应用程序的资源文件引用任何字符串值。 (我将字符串存储在自定义XML文件中作为特殊资源)



以下是所有内容:



自定义类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);
}
}

应用程序类扩展应用程序:

  public class App extends Application {

private static Context mContext;

@Override
public void onCreate(){
super.onCreate();
mContext = getApplicationContext();
}

public static Context getContext(){
return mContext;
}
}

我已经通过该应用程序调试功能和 App.getContext()总是返回一个 上下文 null 。我不知道为什么。 我需要能够从不扩展android生命周期的任何部分的类引用 R.creeps.robodee 。 p>

我将通过构造函数传递上下文,但由于 CreepId 是一个 static enum CreepIDs 实际上并没有被实例化。我只是从主要活动中引用它。



你认为这是最好的解决方案?

解决方案

所以这不是很漂亮,但是我找到了一个解决方案。
在主要活动的 onCreate (现在这个信息甚至被使用的唯一地方),我已经放了以下代码:

  CreepIDs.context = getApplicationContext(); 

由于上下文 static public 无论如何,我在应用程序启动时直接为其分配一个值。我只需要上下文,所以我可以调用资源,它不会用于其他任何东西,所以我不认为这对我来说是一个问题。



最后,使用特殊的 App 类对我来说不起作用。



感谢大家的帮助。


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?

解决方案

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.

这篇关于从非活动类获取资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆