简体   繁体   English

Android:可从库项目绘制到应用程序

[英]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.

相关问题 从Android中的库项目访问可绘制的资源 - Accessing drawable rescources from a library project in Android 如何从Android的库项目中调用驻留在应用程序项目中的drawable? - How to call a drawable which resides in app project from library project in android? 可以在 Android 项目库中共享 drawable 和布局吗? - Can drawable and layouts be shared in a Android Project Library? 如何将库项目中的可绘制对象引用到 android 中的主项目布局中? - How to reference a drawable from a library project into a main project's layout in android? Android:如何在应用程序清单中引用库drawable - Android: How to reference library drawable in app manifest 来自库项目的Jar未在Android应用中解析 - Jar from library project not resolved in Android app 图书馆项目中的Android应用加载了两个应用图标 - Android App from Library project loading two app icons 从Android库项目到使用它的App的独特通信 - Distinct communication from Android Library Project to App using it 从Android库项目引用的Jar未被App引用。 NoClassDefFoundError - Jar referenced from android library project not referenced by App. NoClassDefFoundError Android:如何从库回调到主应用程序项目? - Android: How to callback from a library to the main app project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM