In android, I want to use a string-array resource to create a public static final String in one of my classes. However, it seems I must call getResources() on a Context and I can't find any context at the class level. Is this possible?

public class DBAdapter {

    Resources res = getResources();
    public static final String[] gradeTypes = res.getStringArray(R.array.gradeTypes);
...
有帮助吗?

解决方案

You can't do that. If you need the Context, you can pass it with constructor:

public class DBAdapter{
    private Context mContext;

    public DBAdapter(Context mContext) {
        this.mContext = mContext;
    }
}

But anyway you won't have a chance to use it at the gradeTypes initialization. Why do you need to declare it as final? If you want to use this array at inner classes, you can do it without final modifier.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top