簡體   English   中英

CardView的getResources()上的NullPointerException

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM