Search code examples
androidandroid-fragmentsnullpointerexceptionandroid-resourcesandroid-dialogfragment

Why getResources().getString() return null?


I always get NullPointerException whenever I call:

getApplicationContext().getResources().getStringArray(R.array.days);

I called it from DialogFragment in my Activity. I also tried using getActivity(), but that didn't work for me too. Does anybody have any idea about this problem?


Solution

  • Try this:

    If you write this in a fragment:

    String[] days = getActivity().getResources().getStringArray(R.array.days);
    

    If you write this in an activity:

    String[] days = this.getResources().getStringArray(R.array.days);