如何从Android中未扩展Activity的类访问资源文件?
我有一个在构造函数中传递了上下文的类:
public abstract class AbstractDbAdapter {
protected Context context;
public AbstractDbAdapter(Context context) {
this.context = context;
String email = context.getString(R.string.my_email);
}
它不会在最后一个字符串中出现错误:
package R does not exist context.getString(R.string.my_email);
Package Rdefinetlyexists!我使用相同的调用:
context.getString(R.string.my_email);
在其他类中,它扩展 Activity 没有任何问题。此外,我在 R.java 中看到了 strings:
public static final class string {
...
public static final int my_email=0x7f06001e;
...
}
。我应该怎么做才能让我的代码在类中工作而不扩展 Activity!?
I have a class with context passed in the constructor:
public abstract class AbstractDbAdapter {
protected Context context;
public AbstractDbAdapter(Context context) {
this.context = context;
String email = context.getString(R.string.my_email);
}
It doesn't compile with the error at last string:
package R does not exist context.getString(R.string.my_email);
Package R definetly exists! I use the same call:
context.getString(R.string.my_email);
in other class which extends Activity without any problems. Moreover, I see strings:
public static final class string {
...
public static final int my_email=0x7f06001e;
...
}
in R.java. What should I do to get my code working in the class not extending Activity!?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的活动和 AbstractDbAdapter 必须位于不同的包中。
您只需要导入
R
类。Your activities, and the AbstractDbAdapter must be in different packages.
You just need to import the
R
class.