方法getResources()和上下文未定义类型

use*_*413 6 android getstring android-context

我使用它getString()从string.xml成为String.在我的班级(非活动)不起作用:

  • context.getResources().的getString()
  • getResources().的getString()
  • context.getResources().的getString()

如何获取此类的String?

public class myClass{
     public String[] myInfo(String ID) {
        String myString = getRessources().getString(R.string.myString);
     };
}
Run Code Online (Sandbox Code Playgroud)

Ahm*_*mad 14

你必须打电话context.getResources().getString(),但你必须传入一个上下文才能做到这一点.

您可以创建一个构造函数,例如:

Context context;

public myClass(Context context) {
    this.context = context;
}
Run Code Online (Sandbox Code Playgroud)