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 trial

Android Build an Interactive Story App (Retired) Finishing the User Interface Loading the First Page

My code is giving me an error on the drawable

Here is my code:

package com.example.roshan.mystoryapp.ui;

import android.app.Activity; import android.content.Intent; import android.graphics.drawable.Drawable; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.Log; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView;

import com.example.roshan.mystoryapp.R; import com.example.roshan.mystoryapp.model.Page; import com.example.roshan.mystoryapp.model.Story;

class StoryActivity extends Activity {

 public static final String TAG = StoryActivity.class.getSimpleName();

 public Story mStory = new Story();
 private ImageView mImageView;
 private TextView mTextView;
 private Button mChoice1;
 private Button mChoice2;


 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_story_activity2);

     Intent intent = getIntent();
     String name = intent.getStringExtra("name");
     if (name == null) {
         name = "Friend";
     }
     Log.d(TAG, name);

     mImageView = (ImageView) findViewById(R.id.storyImageView);
     mTextView = (TextView) findViewById(R.id.storyTextView);
     mChoice1 = (Button) findViewById(R.id.choiceButton1);
     mChoice2 = (Button) findViewById(R.id.choiceButton2);
 }

 private void  loadPage(){
   Page page = mStory.getPage(0);
 Drawable drawable = getResources().getDrawable(page.getImageId());
 mImageView.setImageDrawable(drawable);

 }

}

2 Answers

Similar to James I was able to get mine working using ResourcesCompat.

        Drawable drawable = ResourcesCompat.getDrawable(getResources(), page.getImageId(), null);
        mImageView.setImageDrawable(drawable);

Hello,

I'm not seeing anything immediately popping out as wrong, could you post your full error message? If you're getting the warning about getDrawable being deprecated, you do have a few options available to you.

One option is to use the ContextCompat static method.

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.