简体   繁体   English

CardView的getResources()上的NullPointerException

[英]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... 我有一个让我很烦的问题...我需要在一个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;
}

}

I can't figure out how to getResources() 我不知道如何getResources()

For accessing getResources() in normal java class need to use Context. 为了在普通的Java类中访问getResources() ,需要使用Context。 for getting Context add Context parameter to getChoices method: 为了获取上下文,将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.

相关问题 getResources()。getDisplayMetrics()。density上的NullPointerException - NullPointerException on getResources().getDisplayMetrics().density 将 CardView 实现上的 NullPointerException 转换为 RecycleView - NullPointerException on a CardView implementation into a RecycleView 访问class.getResources()时出现NullPointerException - NullPointerException when accessing class.getResources() 带有 RecyclerView 和 CardView 的 java.lang.NullPointerException - java.lang.NullPointerException with RecyclerView and CardView org.eclipse.jface.resource.JFaceResources.getResources上的java.lang.NullPointerException - java.lang.NullPointerException at org.eclipse.jface.resource.JFaceResources.getResources Android Studio CardView onClick java.lang.NullPointerException - Android Studio CardView onClick java.lang.NullPointerException FileNotFoundException与getResources - FileNotFoundException with getResources 无法获取资源 - Unable to getResources java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ void android.widget.CardView.setVisibility(int)” - java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CardView.setVisibility(int)' on a null object reference Android - 问题 getResources() 和 getPackageName() - Android - Problem getResources() and getPackageName()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM