这是我的 res/values-es/strings.xml
文件中的一个字符串:
<string name="jcq1">Text text text textttttttttt</string>
这是我在 java 代码中使用它的地方:
jc.add(new Question("42", "21", "33", "29", 0, getResources().getString(R.string.jcq1), -1));
它给了我这个错误:
The method getResources() is undefined for the type Quiz
其中Quiz
是java代码所在的类的名称。方法 getResources()
的所有其他示例代码似乎都以与我相同的方式使用它,没有问题。我的实现不起作用?
编辑
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()
是 Context 的方法。如果您的 Quiz
类不是 Context
的子类(例如 Activity
等),那么您必须传入有效的 Context
并在该 Context
上调用 getResources()
。
关于java - getResources().getString() 没有为我的类定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16310799/