繁体   English   中英

如何在Android Studio的库模块中使用getResources()函数

[英]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 MethodgetResources()方法,因此您必须获取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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM