![](/img/trans.png)
[英]Android Studio: how to add and use project as library, not as module
[英]How to use getResources() function in a Library module in android studio
我正在为我的主要代码开发一个库模块,该模块应该能够使用GPIO引脚在7段显示器上显示不同的数字,字母和符号。 我使用res
文件夹保存有关在数组中打开哪个元素的信息。 现在,我想将array.xml
导入库模块中的类。
我尝试使用:
public class SevenDisplay{
public SevenDisplay(){
TypedArray figureCode = getResources().getIdentifier("array", "id", "com.library.package");
}
}
但它告诉我:
Cannot resolve method getResources()
有没有办法将数组从array.xml
放入我的库模块中?
现在,您的class
不知道getResources()
是什么。 因此会引发错误。
由于Context
具有作为Instance Method
的getResources()
方法,因此您必须获取context
,因此为此在类的构造函数中为context添加一个参数,如下所示:
public class SevenDisplay {
public SevenDisplay(Context context) {
TypedArray figureCode = context.getResources().getIdentifier("array", "id", "com.library.package");
}
}
当您创建此实例时,然后传递context
如下所示:
SevenDisplay sevendisplay = new SevenDisplay(YourActivity.this);
这将帮助您,并且错误将被消除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.