Domanda

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);
...
È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top