mwieczorek
mwieczorek

Reputation: 2252

Getting int value of Resource from Application Class

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?

Upvotes: 1

Views: 48

Answers (1)

Nir Duan
Nir Duan

Reputation: 6392

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.

Upvotes: 2

Related Questions