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

Denholm Bishop
Denholm Bishop
1,845 Points

Cannot resolve symbol 'RED'!

I have been following the instructions on the video but then Android studio shows an error. I have been typing this line:

relativeLayout.setBackgroundColor(color.RED);

When I hover over 'RED' it reads: Cannot resolve symbol 'RED' I tried replacing it with other colours, blues, yellows, whites, even random hexadecimal values. Nothing worked. While I was typing in 'color.white' I saw a suggestion to correct it to 'android.R.color.white' and once again it also showed an error. I'm completely stuck.

I'm using Android studio 0.8.2 which I was I believe was being used in the android 2014 course.

1 Answer

relativeLayout is not an object, so you will get an error "non-static methodcannot be referenced from a static context".

        //you will get an error
        relativeLayout.setBackgroundColor(color.RED);

To fix this error you need to create an object first from the relativeLayout

        RelativeLayout layout = (RelativeLayout) findViewById(R.id.someLayout);
        layout.setBackgroundColor(Color.GREEN);

Notice how i used layout.setBackgroundColor, the word "layout" is not important as I can replace it with another word and it will still work.

        RelativeLayout asdfg = (RelativeLayout) findViewById(R.id.someLayout);
        asdfg.setBackgroundColor(Color.GREEN);

The only thing that is important is that it matches the variable name in the line above it. If you post your code her, I can help you specifically with your code.

Denholm Bishop
Denholm Bishop
1,845 Points

Thanks but the relativeLayout object isn't the problem, it's the 'color'. Android studio highlighted the 'RED' part as an error as if it doesn't recognise it. If I 'alt+enter' on it, it just shows three options - Create class 'RED', Create inner class 'RED' and Rename reference.

Paste your code.

Also, be sure Color is uppercase.

Denholm Bishop
Denholm Bishop
1,845 Points

While I was copying the code, I noticed 'color' was in lowercase so I changed it to uppercase and the 'cannot resolve symbol' error moved to 'Color' instead of 'RED' so I pressed 'alt+enter' on it and the error disappeared for some reason. I'm not sure what I did wrong before and what alt+enter did to fix the error. Anyway, here's the code below.

public class FunFactsActivity extends Activity { private FactBook mFactBook = new FactBook();

@Override
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);

    final RelativeLayout layout = (RelativeLayout) findViewById(R.id.relativeLayout);

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        String fact = mFactBook.getFact();


        //Update the label with our dynamic fact
        factLabel.setText(fact);
        layout.setBackgroundColor(Color.RED);
        }
    };
    showFactButton.setOnClickListener(listener);
}

}

It appears using layout instead of relativeLayout and an uppercase Color. Sometimes when you make changes it doesn't recognize a change so you have to rebuild your project (build > rebuild). Alt-clicking may have rebuilt your project. Either way, congrats on fixing your problem!