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 Build a Simple Android App (2014) Improving Our Code Adding More Colors

parseColour and setBackgroundColour are red

So i can't run my app because in my MainActivity.java and ColourWheel.java there are 2 errors. BTW i didn't rename MainActivity.java and wrote Colours instead of Colors. Canadian way.

MainActivity.java

package com.apps.tegh.funfacts;

import android.app.Activity;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.Random;


public class MainActivity extends Activity {

    private FactBook mFactBook = new FactBook();
    private ColourWheel mColourWheel = new ColourWheel();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 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 relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String fact = mFactBook.getFact();
                factLabel.setText(fact);

                int colour = mColourWheel.getColour();
                relativeLayout.setBackgroundColour(colour);

            }
        };
        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.menu_main, menu);
        return true;
    }
  }

ColourWheel.java

package com.apps.tegh.funfacts;

import java.util.Random;

/**
 * Created by Tegh SM on 6/28/2015.
 */
public class ColourWheel {

        // Member variable (properties about the object)

        public String[] mColours = {
            "#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
    };

    public int getColour() {
        String colour = "";
        // Randomly select a fact
        Random randomGenerator = new Random();  // Construct a new random generator
        int randomNumber = randomGenerator.nextInt(mColours.length);

        colour = mColours[randomNumber];

        int colourAsInt = colour.parseColour(colour);

        return colourAsInt;
    }

}

Sorry for posting this off-topic, but I'm helping Treehouse investigate a potential issue with their webserver. Please follow this link and leave a comment saying if you have experienced this webserver issue or not.

https://teamtreehouse.com/forum/attention-treehouse-webserver-issues-please-read

Thank you.

1 Answer

Jon Kussmann
PLUS
Jon Kussmann
Courses Plus Student 7,254 Points

When you write:

String colour = "";

That is completely fine, but when you want to call specific methods, you have to use the spelling "color". So:

int colourAsInt = colour.parseColor(colour);
relativeLayout.setBackgroundColor(colour);

You can leave all other "colours" as you wish.

Thanks Jon Kussmann it worked plus I understand now. :D