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...
有帮助吗?

解决方案

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.

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top