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 trialJulian Sanchez
Courses Plus Student 2,108 PointsChanging 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
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
3,931 PointssetBackgroundColor 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
Courses Plus Student 2,108 PointsJulian Sanchez
Courses Plus Student 2,108 PointsThank you! I tried again using your code and it worked! :)