简体   繁体   中英

NullPointerException on getResources() for a CardView

I have a problem that bugs me a lot... I need to call a string array in a normal java class in an Android project, but for the love of God I can't figure out how to getResources() over here... I tried some getContext ideas from Google but to no avail... The java file is used to load a CardView's elements with data... Please ask me for more details if needed...

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;
}

}

I can't figure out how to getResources()

For accessing getResources() in normal java class need to use Context. for getting Context add Context parameter to getChoices method:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM