该方法getResources()和上下文是未定义的类型 [英] The method getResources() and context is undefined for the type

查看:556
本文介绍了该方法getResources()和上下文是未定义的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的是的getString()成为字符串从string.xml。 在我的课(非活动)不工作:

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

如何获得字符串这一类?

 公共类MyClass的{
     公众的String [] MyInfo的(字符串ID){
        字符串MyString的= getRessources()的getString(R.string.myString)。
     };
}
 

解决方案

您必须调用 context.getResources()的getString(),但你必须传入为了一个背景下做到这一点。

您可以创建一个构造,这需要的参数,例如:

 上下文的背景下;

公共MyClass的(上下文的背景下){
    this.context =背景;
}
 

I use the getString() to become String from string.xml. In my class (non-activity) does not work:

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

How do I get the String to this class?

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

解决方案

You have to call context.getResources().getString(), but you have to pass in a context in order to do that.

You can create a constructor, that takes that parameter for example:

Context context;

public myClass(Context context) {
    this.context = context;
}

这篇关于该方法getResources()和上下文是未定义的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆