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

Why is the symbol R red in the code

Hi everyone, the symbol R is highlighted red in my code, I will appreciate it if someone could provide a fix to this problem

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fun_facts);

    //Declare our View Variables and assign the Views from the layout file//
   final TextView factLabel = (TextView) findViewById(R.id.factTextView);
    Button showFactButton = (Button) findViewById(R.id.showFactButton);


    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String[] facts = { "Ants stretch when they wake up in the morning.",
                    "Ostriches can run faster than horses.",
                    "Olympic gold medals are actually made mostly of silver.",
                    "You are born with 300 bones; by the time you are an adult you will have 206.",
                    "It takes about 8 minutes for light from the Sun to reach Earth.",
                    "Some bamboo plants can grow almost a meter in just one day.",
                    "The state of Florida is bigger than England.",
                    "Some penguins can leap 2-3 meters out of the water.",
                    "On average, it takes 66 days to form a new habit.",
                    "Mammoths still walked the earth when the Great Pyramid was being built." };

            // the button was clicked, so update the fact label with a new fact
            String fact = "";
            // Randomly select a fact
            Random randomGenerator = new Random();
            int randomNumber = randomGenerator.nextInt(3);


            /* Convert the randomNumber to a text fact
            * 0 = Ants stretch when they wake up in the morning.
            * 1 = Ostriches can run faster than horses.
            * 2 = Olympic gold medals are actually made mostly of solver.
             */

                if (randomNumber== 0) {
                    fact = "Ants stretch when they wake up in the morning.";
            }

            else if (randomNumber== 1) {
                fact = "Ostriches can run faster than horses.";
            }


            else if (randomNumber == 2) {
                fact = "Olympic gold medals are actually made mostly of silver.";
            }
            else {
                  fact = "Sorry, there was an error";
                }





            factLabel.setText(fact);
        }
    };
    showFactButton.setOnClickListener(listener);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.fun_facts, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

1 Answer

Oh Hey, This is a really crappy error lol. You can google all day long and try everything and if you are like me. Not a single one will work. So let me tell you what is happening.

This relates to the build process. When you open eclipse by default it builds all projects. When you build a project after the fact this can still happen.

Everyone will tell you to clean your your project and re-build etc etc etc. Some will say if it's during android, that it's because your pictures used in your app have names with capital letters. Yeah, maybe some of this is relevant but I have hit this for years and never found a single online resource that works. Hence while I shall explain it here.

The issue is that your project has yet to build to this point of completion (the reason my vary as to why). During the build it begins building the project. If there is a detrimental error during build. The build doesn't finish. When it doesn't finish. It doesn't get to the point where it resolves all resources. When it doesn't get this far, all the R.xxx never get resolved. Unfortunately IDE's are very poor at reporting this. Cleaning your projects or doing almost anything else will not matter. What this is saying is that your build failed before it go to a certain point. This point being resolving the R's.

So how do you know if your build worked or not??? Let me tell you.

In eclipse for an example,
Open the project. In the project should be a gen folder. Just under the bin folder. Click the twisty and expand it. You will see two folders. "Android" and "com" -- Assuming your projects reverse domain starts with com. Now, say my project is com.whatever.whatever etc etc Click on whatever folder that isn't android put it that way. If there is no android than click whatever folder is there. Keep clicking all the twisties until you get to the .java files. There should be any number of java files but the one you want is R.java If there is NO R.java. Your build failed. This is why your project can't resolve the R.xxx resources.

Ok, so it failed building. How do I fix it??? Let me tell you.

Go to the project and unselect build automatically. In eclipse or whatever IDE, find the "Problems" tab. Now right click and delete all the problems. Remove the entire list. Then go right click on your project and click, build project.

By default all projects are build automatically. Odds are your problems tab has 1000 + errors and warnings. I have a million not working projects. So mine is that way anyway. We don't care about any of those except the ones that relate to our individual project. Although if you are like me it can be impossible to identify only that projects errors. But there is way.

Look at the Problems tab. Right click on it and remove all errors. All of them. Delete the entire list. Now, go to your Projects tab and select "Build Automatically". make sure it's not checked. Now, right click on the actual project. Select Build. This will regenerate all errors pertaining to just your project. So we can identify only the problems with your project.

Now, if you look at the issues with a red x. It will point out a specific problem in a specific location. At least we hope, it may take some digging. But the errors there are WHY your project isn't building. Fix those errors one by one and re-buildinjg your project until under gen folder, you see the R.java. When you see that your resources got to the point where they were resolved and you will no longer see the error.

now yes, if you put a file in the res drawable folders. That has a capital letter in the name This could cause it to happen. Although that is what the internet says. I have capital letters in mine and I still get the R.java folder so everything resolves so whatever.

let me know what errors you find and if you can't resolve them. I can help you resolve them and thus, fix the issue. regardless of what errors. the answer is in the problems sections for that specific build. If cleaning fixes it than ok. But it never has for me and I have built dozens and dozens. A simple clean has never worked. But it may for you or so the internet would say. Anyway, good luck. Thanks!