从Application Class获取Resource的int值(Getting int value of Resource from Application Class)

编程入门 行业动态 更新时间:2024-10-23 03:18:43
从Application Class获取Resource的int值(Getting int value of Resource from Application Class)

我正在设置一个通知类,它需要独立于任何Activity,但是通过我的主Application类调用。 我无法访问资源。

Application类通过context.getApplicationContext()获取其上下文,并将其传递给我的通知类的构造函数:

NotificationService notificationService = new NotificationService(context, notifications);

我需要在Drawables中设置小图标,当我添加R.drawable.tv_logo_notext_bw资源时,编译器无法找到它:

NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setSmallIcon(R.drawable.tv_logo_notext_bw);

显然,它需要一个上下文,但是上下文被传递给我的通知类和NotificationCompatBuilder类的构造函数,所以我不明白为什么编译器看不到drawable。

我错过了一些关键步骤吗?

I'm setting up a notification class that needs to be independent of any Activity, but called through my main Application class. I'm having trouble accessing resources.

The Application class gets its context via context.getApplicationContext(), and this is passed to the constructor of my notification class:

NotificationService notificationService = new NotificationService(context, notifications);

I need to set the small icon from my Drawables, and when I add the R.drawable.tv_logo_notext_bw resource, the compiler cannot locate it:

NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setSmallIcon(R.drawable.tv_logo_notext_bw);

Obviously, it needs a context, but the context was passed to the constructor of both my notification class and the NotificationCompatBuilder class, so I don't understand why the compiler cannot see the drawable.

Am I missing some crucial step?

最满意答案

资源ID是公共静态文件,由Gradle和IDE自动生成,这意味着您应该能够从项目的任何部分调用,您的代码看起来很好。

尝试下一步“刷新”IDE(Android Studio)

1.尝试重建项目Build | 重建 2.同时尝试选择文件| 使高速缓存/重新启动无效。

Resource Id's are public static filed and being auto-generated by your Gradle & IDE, this means you should be able to call the from any part of you project, your code looks fine.

Try the next step to "Refresh" your IDE (Android Studio)

1. Try to rebuild your project Build | Rebuild 2. Also try Choose File | Invalidate Caches/Restart.

更多推荐

本文发布于:2023-07-16 12:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128573.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Class   Application   int   Resource

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!