Ruben Pita
Ruben Pita

Reputation: 53

Android - Error with getApplicationContext

I'm trying to make an arraylist so then I can randomly pick a string from a list. I visited this question and on this question there was the perfect answer for what I wanted to do. Although on this piece of code:

String[] colorsTxt = getApplicationContext().getResources().getStringArray(R.array.colors);
    List<Integer> colors = new ArrayList<Integer>();
    for (int i = 0; i < colorsTxt.length; i++) {
        int newColor = Color.parseColor(colorsTxt[i]);
        colors.add(newColor);
    }

I can't find why getApplicationContext gives me an error, someone has an idea?

Thanks

Upvotes: 1

Views: 167

Answers (1)

Vedran Kopanja
Vedran Kopanja

Reputation: 1299

Because you said you are using it in a fragment, you need to call getActivity().getResources()... to access it :).

Upvotes: 3

Related Questions