Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Simple Android App Improving Our Code Dynamically Changing the Background Color

Julian Sanchez
PLUS
Julian Sanchez
Courses Plus Student 2,108 Points

Changing background color exercise

When trying to do the changing background color exercise I get this error:

./MealActivity.java:10: error: non-static method setBackgroundColor(int) cannot be referenced from a static context RelativeLayout.setBackgroundColor(Color.GREEN); ^ 1 error

What I coded in order to change the background color is this:

RelativeLayout.setBackgroundColor(Color.GREEN);

This is whole code:

import android.os.Bundle; import android.widget.TextView;

public class MealActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_meal);
    RelativeLayout.setBackgroundColor(Color.GREEN);

    TextView foodLabel = (TextView) findViewById(R.id.foodTextView);
    TextView drinkLabel = (TextView) findViewById(R.id.drinkTextView);
    RelativeLayout mealLayout = (RelativeLayout) findViewById(R.id.mealLayout);
}

}

What am I doing wrong? I've already done this correctly in Android Studio but there's something wrong here.

Thank you

MealActivity.java
import android.os.Bundle;
import android.widget.TextView;

public class MealActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_meal);
      RelativeLayout.setBackgroundColor(Color.GREEN);

        TextView foodLabel = (TextView) findViewById(R.id.foodTextView);
        TextView drinkLabel = (TextView) findViewById(R.id.drinkTextView);
        RelativeLayout mealLayout = (RelativeLayout) findViewById(R.id.mealLayout);
    }
}

1 Answer

Melodie 1
Melodie 1
3,931 Points

setBackgroundColor is not a static method , that means it's defined on the instance level and i order to be able to use it, you must use a reference variable that's pointing to the object that has this method . This should fix your error @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_meal);

TextView foodLabel = (TextView) findViewById(R.id.foodTextView);
TextView drinkLabel = (TextView) findViewById(R.id.drinkTextView);
RelativeLayout mealLayout = (RelativeLayout) findViewById(R.id.mealLayout);
mealLayout.setBackgroundColor(Color.GREEN);

}

Julian Sanchez
Julian Sanchez
Courses Plus Student 2,108 Points

Thank you! I tried again using your code and it worked! :)