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 with Java Improving Our Code Using the New Colors

Dayan Rodriguez
Dayan Rodriguez
1,286 Points

When Changing colors my app crashes

//This is how I call the method from the main activity


int color = colorBook.getColor();
relativeLayout.setBackgroundColor(color);

//This is the ColorBook class

public class ColorBook {
    // Fields or Member Variables - Properties about the object
    private String[] colors = {
            "#39add1", // light blue
            "#3079ab", // dark blue
            "#c25975", // mauve
            "#e15258", // red
            "#f9845b", // orange
            "#838cc7", // lavender
            "#7d669e", // purple
            "#53bbb4", // aqua
            "#51b46d", // green
            "#e0ab18", // mustard
            "#637a91", // dark gray
            "#f092b0", // pink
            "#b7c0c7"  // light gray
    };

    // Methods - Actions the object can take
    int getColor() {
        // Randomly select a color
        Random randomGenerator = new Random();
        int randomNumber = randomGenerator.nextInt(colors.length);
        int color = Color.parseColor(colors[randomNumber]);
        return color;
    }
} 

[MOD: edited code block - srh]

Tim Herron
Tim Herron
16,837 Points

I too had it crash every time it tried to change random color. On FunFactsActivity.java, when you created a new object ColorWheel, I personally for got to add "= new ColorWheel". I originally had it created like factTextView, showFactButton, and relativeLayout. But I should have created it like factBook.

public class FunFactsActivity extends AppCompatActivity {
    private FactBook factBook = new FactBook();
    private ColorWheel colorWheel;
    //Declare our view variables
    private TextView factTextView;
    private Button showFactButton;
    private RelativeLayout relativeLayout;

But after looking all throughout my code, I found my mistake. I don't know if you made the same mistake, but maybe this will help someone.

public class FunFactsActivity extends AppCompatActivity {
    private FactBook factBook = new FactBook();
    private ColorWheel colorWheel = new ColorWheel();
    //Declare our view variables
    private TextView factTextView;
    private Button showFactButton;
    private RelativeLayout relativeLayout;
Dayan Rodriguez
Dayan Rodriguez
1,286 Points

Thank you so much Tim Herron, It was just that.

1 Answer