Question

Here is a string in my res/values-es/strings.xml file:

<string name="jcq1">Text text text textttttttttt</string>

Here is where I use it in my java code:

jc.add(new Question("42", "21", "33", "29", 0, getResources().getString(R.string.jcq1), -1));

It is giving me this error:

The method getResources() is undefined for the type Quiz

Where Quiz is the name of the class where the java code resides. All other example code for the method getResources() seem to use it the same way I have without problem. What isn't my implementation of it working?

EDIT

public class Quiz {

    public Quiz(Context c) {

                    jc.add(new Question("42", "21", "33", "29", 0, c.getResources().getString(R.string.jcq1), -1));
                    // 300 other lines pretty much identical to the one above follows...
Was it helpful?

Solution

getResources() is a method of Context. If your Quiz class is not a subclass of Context (such as Activity, etc), then you must pass in a valid Context and call getResources() on that Context.

OTHER TIPS

The examples you're looking at probably expect this code to be used in an Activity class, which your Quiz class clearly is not. You need a Context for this method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top