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

Problem linking resource files between packages

This is my first app which I'm having some issues with. Its a simple app made up of 2 equal sized images which change when clicked.

There seems to be an issue with the R id's. My first error appears at setContentView(R.layout.activity_my). The error says "cannot resolve symbol R". Ive checked all my id's match. Not sure what I'm doing wrong.

"""java public class MyActivity extends Activity {

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

    //Declare elements
    ImageButton topButton = (ImageButton) findViewById(R.id.topButton);
    ImageButton bottomButton = (ImageButton) findViewById(R.id.bottomButton);

    //Set up resources for the top Image Button
    final Drawable[] topPhotos = new Drawable[]{
            getResources().getDrawable(R.drawable.photo1),
            getResources().getDrawable(R.drawable.photo2),
            getResources().getDrawable(R.drawable.photo3),
            getResources().getDrawable(R.drawable.photo4),};

    //Set up resources for the bottom Image Button
    final Drawable[] bottomPhotos = new Drawable[]{
            getResources().getDrawable(R.drawable.photo5),
            getResources().getDrawable(R.drawable.photo6),
            getResources().getDrawable(R.drawable.photo7),
            getResources().getDrawable(R.drawable.photo8),};

    //Clicking the top button shows a random photo
    topButton.OnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Random random = new Random();
            int randomNumber = random.nextInt(topPhotos.length);
            Drawable topPhotos = new topPhotos[randomNumber];
            topButton.setImageDrawable(topPhotos);}
            };


}

} """

6 Answers

Check your imports and make sure you have added "import android.R;" if that is there, then try to do Build - > Clean Project and see if that helps.

Hi Gloria Apologies for the delay I've only just managed to try your suggestion.

The import statement wasn't there so I added that and cleaned the project. The error for R has now gone. However, when I rebuilt there was a Gradle build error.As below:

""" C:\Users\Chris\AndroidStudioProjects\PleaseWork\app\src\main\res\drawable-mdpi\photo7.JPG Error:Error: Invalid file name: must contain only lowercase letters and digits ([a-z0-9_.]) """

I can't make sense of this as they are only JPEG images and saved all in lower case ie. photo7

What could it be?

Many thanks

Holly

Hi Holly, I think the problem is the .JPG, try making it lowercase, .jpg.

Hi

That helped. I just rebuilt. I now have an error on activity_my. (Its not going well!)

''' setContentView(R.layout.activity_my) '''

I tried deleting and re-typing, trying to find it in the list that appears, but its not there. Any ideas?

All the photo id's are all shown in red too within the following GetResources statements:

''' final Drawable[] topPhotos = new Drawable[]{ getResources().getDrawable(R.drawable.photo2), getResources().getDrawable(R.drawable.photo3), getResources().getDrawable(R.drawable.photo4) '''

Have you created the layout you are asking for? "activity_my" ? If no then you need to create it, I assume that is why it does not appear when you type it.

I created it at the same time I created the project. Its where the layout code is. I have set up id's for the image buttons I'm using. It worked at one point so not sure whats changed. The name of the files are the same.

When you hover you mouse above the red texts. What message do you get?

I get a message saying "cannot resolve symbol 'activity_my'"

Can you show me a screenshot of where you have placed activity_my?

I'm not sure how I attach the screenshot to this comment....

Check this How to markdown within posts, it shows how.

Hi Gloria

I have been working on another project and the same issues started to happen all over again. I followed the steps you suggested. This time the import statement for R didn't resolve the issue. However, when I deleted this statement the errors went away. So I just went back into my other project and deleted that statement and it all refreshed ok. Not sure why I'm getting these random intermittent issues and it would be good to fully understand to get to the route cause.

However, just wanted to let you know that for now the issues are ok, so I will leave you in peace! Thanks for all your help :)

You are welcome, i'm glad it is working now.