对于CardView的getResources()的NullPointerException

时间:2015-02-22 15:30:35

标签: java android nullpointerexception android-cardview

我有一个问题让我烦恼很多......我需要在Android项目的普通java类中调用一个字符串数组,但是为了爱上帝,我无法弄清楚如何获取getResources()这里......我尝试了一些来自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;
}

}

1 个答案:

答案 0 :(得分:2)

  

我无法弄清楚如何获取资源()

在普通java类中访问getResources()需要使用Context。获取Context将Context参数添加到getChoices方法:

public List<Choice> getChoices(Context mContext) {
    //...
    ChoiceArray = mContext.getResources().getStringArray(R.array.group_iteme);
   //....
    return  choices;
}