문제

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