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 trialRay Kennedy
Courses Plus Student 2,982 PointsDrawable is deprecated How should I structure my code
I am following the story application and when I write the following code
private void loadPage() { Page page = mStory.getPage(0);
Drawable drawable = getResources().getDrawable(page.getImageId());
mImageView.setImageDrawable(drawable);
String pageText = page.getText();
//This will add the name if placeholder included
pageText = String.format(pageText, mName);
mTextView.setText(pageText);
mChoice1.setText(page.getChoice1().getText());
mChoice2.setText(page.getChoice2().getText());
}
this "get drawable" has a line drawn through it.
I've tried it like this: Drawable drawable = getResources().getDrawable(page.getImageId(), null); but that makes the system bark errors at me. Any help would be greatly appreciated.
1 Answer
James Simshaw
28,738 PointsHello,
One way to make it so this is compatible both backwards and forwards is to use the ContextCompat class from the support library.
For example change
Drawable drawable = getResources().getDrawable(mCurrentWeather.getIconId());
to
Drawable drawable = ContextCompat.getDrawable(this, mCurrentWeather.getIconId());
You also need to make sure you're importing the right class, but I believe just using the optimize your imports command should bring in the right one as long as the support library is included in your build.gradle file.