[英]Android: Drawable from library project to app
I'm currently creating a payment SDK for android, as such I want to send some images from the sdk/library project to the actual application based on different situations. 我目前正在为Android创建一个付款SDK,因此我想根据不同情况从sdk / library项目向实际应用程序发送一些图像。 I want the users of the application to use the drawables, but I dont want them to set it manually(ie get it directly from the SDK's drawable folder), the SDK should automatically choose what image to use based on difference situations.
我希望应用程序的用户使用可绘制对象,但是我不希望他们手动设置它(即直接从SDK的可绘制文件夹中获取它),SDK应该根据不同情况自动选择要使用的图像。
Currently what I have is a DAO class which when initilized have something like: 目前,我拥有的是一个DAO类,在初始化时它具有类似以下内容:
case (MASTERCARD):
this._logo = getResources().getDrawable(R.drawable.mastercard_securecode);
and then in the application I want to be able to fetch that drawable and use it in an ImageView like this: 然后在应用程序中,我希望能够获取该drawable并在ImageView中使用它,如下所示:
img.setImageDrawable(DAOObject.getLogo());
However when running this I get 但是当运行此我得到
06-15 20:25:18.933: W/dalvikvm(625): VFY: unable to resolve static field 6 (mastercard_securecode) in LPackagePath/R$drawable;
06-15 15:25:18.933:W / dalvikvm(625):VFY:无法解析LPackagePath / R $ drawable中的静态字段6(mastercard_securecode);
followed by a nullpointer exception as getLogo will return null due to the above error. 后跟一个nullpointer异常,因为上述错误,getLogo将返回null。
Anyone know how to implement this properly so I dont get the above errors and still be able to implement it this way where the application gets the drawable sent dynamically as such? 任何人都知道如何正确实施此操作,这样我就不会出现上述错误,并且仍然能够以这种方式在应用程序获取可动态发送的drawable的情况下实现它?
Any help is much appreciated! 任何帮助深表感谢!
Okay so I figured this out finally. 好吧,我终于明白了。
The problem really was Context. 问题确实是上下文。 I needed to pass the context from the application into the SDK and then I could get the images properly by doing
我需要将上下文从应用程序传递到SDK中,然后通过执行以下操作可以正确获取图像
context.getResources().getDrawable(R.drawable.mastercard_securecode);
Secondly I needed to import the jar file in a library project and then import the images in that second library project so that the SDK it self is closed source but I still had the ressources available. 其次,我需要将jar文件导入一个库项目中,然后将图像导入该第二个库项目中,以便它本身的SDK是封闭源,但我仍然有可用的资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.