我正在设置一个通知类,它需要独立于任何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.
更多推荐
发布评论