[英]NullPointerException on getResources().getDisplayMetrics().density
[英]NullPointerException on getResources() for a CardView
我有一个让我很烦的问题...我需要在一个Android项目的普通Java类中调用字符串数组,但是出于对上帝的爱,我无法在这里弄清楚如何获取Resources()。我尝试了Google的一些getContext想法,但无济于事...该Java文件用于加载CardView元素中的数据...如果需要,请询问我更多详细信息...
public class ChoicesManager {
private static String[] ChoiceArray;
private static ChoicesManager mInstance;
private List<Choice> choices;
public static ChoicesManager getInstance() {
if (mInstance == null) {
mInstance = new ChoicesManager();
}
return mInstance;
}
public List<Choice> getChoices() {
if (choices == null) {
choices = new ArrayList<Choice>();
ChoiceArray = mInstance.getResources().getStringArray(R.array.group_iteme); //HOW DO I FIX THIS
for (String choiceName : ChoiceArray) {
Choice choice = new Choice();
choice.name = choiceName;
choice.imageName = choiceName.replaceAll("\\s+","").toLowerCase();
choices.add(choice);
}
}
return choices;
}
}
我不知道如何getResources()
为了在普通的Java类中访问getResources()
,需要使用Context。 为了获取上下文,将Context参数添加到getChoices
方法中:
public List<Choice> getChoices(Context mContext) {
//...
ChoiceArray = mContext.getResources().getStringArray(R.array.group_iteme);
//....
return choices;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.