如何的onCreate之前的静态字符串使用的getString()()? [英] How to use getString() on static String before onCreate()?

查看:230
本文介绍了如何的onCreate之前的静态字符串使用的getString()()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的getString()来获得资源的字符串将其分配到一个String数组我的活动创建之前:

I am trying to use getString() to get an String from resources to assign it to an String array before my activity is created:

private static final String[] MenuNames = {
    Resources.getSystem().getString(R.string.LCMeterMenu),
    Resources.getSystem().getString(R.string.FrecMenu),
    Resources.getSystem().getString(R.string.LogicAnalyzerMenu),
    "Prueba con achartengine",
    Resources.getSystem().getString(R.string.BrazoMenu)
};

当我使用 Resources.getSystem()的getString(R.string.LCMeterMenu),Eclipse不抱怨,但我得到在运行时出现错误:

When I use Resources.getSystem().getString(R.string.LCMeterMenu), Eclipse doesn't complain but I get an error at runtime:

产生的原因:android.content.res.Resources $ NotFoundException:字符串资源ID#0x7f0a000a

Caused by: android.content.res.Resources$NotFoundException: String Resource ID #0x7f0a000a

但是,如果我把里面的的onCreate()

But if I put inside onCreate():

Log.i("StringR", "String: " + getString(R.string.LCMeterMenu));

我得到的字符串,但我不能把它分配给我定义之前的最后字符串。如果我只用的getString()的onCreate()我得到的和静态的错误消息。如何使用资源之前的onCreate()全局变量?

I get the String but I can't assign it to the final String I defined before. If I use only getString() before onCreate() I get and static error message. How can I use resources before onCreate() for global variables?

推荐答案

您不能初始化从资源静态最后字段;该领域需要在类被初始化时被初始化,并且发生该应用程序资源已结合在运行时间之前。 (顺便说一句,你不能使用 Resources.getSystem()的原因是,资源对象,你得到这样只包含系统资源,没有任何应用程序的资源。)

You cannot initialize a static final field from resources; the field needs to be initialized at the time the class is initialized and that happens before the application resources have been bound at run time. (By the way, the reason you cannot use Resources.getSystem() is that the Resources object you obtain that way contains only system resources, not any application resources.)

如果你需要这些字符串的可用应用程序的资源绑定前,唯一可行的做法是直接把字符串到code。然而,Android的方式将组织您的code这样的初始化只需要发生期间(或之后)的onCreate()。只是初始化字符串数组中的的onCreate()键,不用担心制作领域的静态或决赛。

If you need those strings available before the application resources are bound, the only practical thing to do is to put the strings into the code directly. However, the "Android way" would be to organize your code so initialization only needs to happen during (or after) onCreate(). Just initialize the string array in onCreate() and don't worry about making the fields static or final.

如果您不希望字符串数组与特定活动相关联,那么你也可以继承应用程序并读取资源数组内的应用程序类的的onCreate()方法。 (你还需要声明您的自定义应用程序类的清单中。)然而,的文档推荐针对这样的方法。 (因为数组是私有的,我怀疑这是密切相关的一个活动,无论如何,所以使用应用子似乎没有必要。)

If you don't want the string array to be associated with a particular activity, then you can subclass Application and read the array from resources inside the application class's onCreate() method. (You also need to declare your custom application class in the manifest.) However, the docs recommend against such an approach. (Since the array is private, I suspect that it is closely tied to a single activity anyway, so the use of an Application subclass doesn't seem warranted.)

另一种方法是定义一个单例类阵列。该单存取功能,则需要一个上下文,以便它可以检索资源,如果必要的:

An alternative is to declare a singleton class for your array. The singleton accessor function then needs a Context so it can retrieve the resources if necessary:

public class StringArray {
    private static String[] theArray;
    public static String[] getArray(Context context) {
        if (theArray == null) {
            theArray = context.getResources().getStringArray(R.array.my_strings);
        }
        return theArray;
    }
}

(这里假设字符串数据在&LT定义;字符串数组> 在答复资源像@JaiSoni建议)再次,成员字段不能声明最后

(This assumes the string data are defined in a <string-array> resource like @JaiSoni suggested in his answer.) Once again, the member field cannot be declared final.

这篇关于如何的onCreate之前的静态字符串使用的getString()()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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