Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDavid Palczewski
2,557 PointsAS has ".getDrawable" crossed out. It says it is deprecated.
When declaring the drawable, AS has ".getDrawable" crossed out. It says it is depreciated. Is there a different method to accomplish this for newer APIs? Or should this one be used for compatibility?
4 Answers
James Simshaw
28,738 PointsHello,
You can use ContextCompat's getDrawable from the support library and pass a null in as the second parameter. More info on the deprecation and what to do can be found on this Google+ post here.
Jordan Cox
8,930 PointsHi you make it sound so obvious, but unfortunately I am a NOOB literally copying line for line this lesson with absolutely no skills or knowledge whatsoever. Can you give the correct code please?
Drawable drawable = getResources().getDrawable(mCurrentWeather.getIconId()); mIconImageView.setImageDrawable(drawable);
the ".getDrawable" is crossed out with the depreciated message.
Thanks for any help!!
James Simshaw
28,738 PointsChange
Drawable drawable = getResources().getDrawable(mCurrentWeather.getIconId());
to
Drawable drawable = ContextCompat.getDrawable(this, mCurrentWeather.getIconId());
You're passing this in as the first parameter because it is requiring a context, which if you're in an Activity, the Activity is the context. Once you change that, optimize your imports and it should work. If not, let me know and I can help troubleshoot some more.
Cristian Daniel Marquez Barrios
8,900 PointsMy solution for this was:
Drawable drawable = ResourcesCompat.getDrawable(getResources(), mCurrentWeather.getIconId(), null);
mIconImageView.setImageDrawable(drawable);
Gavin Ralston
28,770 PointsWhich is fine, because you're not looking to import a themed drawable, just the resource itself.
ContextCompat would matter more if you wanted to bring along themed drawables.
Jordan Cox
8,930 PointsOK thank you :) I did try that first from the google doc you posted, but I must have mispelled or done something stupid :) It seems to be working!
I also don't really understand "optimise your imports"? You mean alt+enter?
Thanks again!
James Simshaw
28,738 PointsCtrl + Alt + O
Which optimizes your import statements in one go. In this case though, Alt + Enter also would have worked since it would just fix the one problem.
Jordan Cox
8,930 PointsThanks again! I don't know how to give you some thumbs up or extra credit for being an android genius (like myself ;) ) but let it be known that James Simshaw is the master!
Jordan Cox
8,930 PointsJordan Cox
8,930 PointsHi you make it sound so obvious, but unfortunately I am a NOOB literally copying line for line this lesson with absolutely no skills or knowledge whatsoever. Can you give the correct code please?
Drawable drawable = getResources().getDrawable(mCurrentWeather.getIconId()); mIconImageView.setImageDrawable(drawable);
the ".getDrawable" is crossed out with the depreciated message.
Thanks for any help!!