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 (2014) Basic Android Programming Generating a Random Number

CD Lim
CD Lim
4,782 Points

factLabel is highlighted in pink

Hi my factLabel is highlighted in pink and I can't convert the randomNumbers into strings

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 files final TextView factLabel = (TextView) findViewById(R.id.factTextView); final Button showFactButton = (Button) findViewById(R.id.showFactButton); final View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View view){ // the button was clicked, it will update the with some new facts String fact = ""; // Randomly select a fact Random randomGenerator = new Random(); //construct a new Random generator int randomNumber = randomGenerator.nextInt(3); /* convert randomNumber in a text fact * 0 = Ants stretch when they wake up in the morning * 1 = Ostriches can run faster than horses * 2 = Olympic Gold metals are usually made mostly of silver */ // if randomNumber equals to zero then if (randomNumber == 0) { // set fact equals to ants fact fact = "Ants stretch when they wake up in the morning"; }

            else if (randomNumber == 1) {
                fact = "Ostriches can run faster than horses";

            else if (randomNumber == 2) {
                fact = "Olympic Gold metals are usually made mostly of silver";

            else {
                fact = "Sorry there was an error";
            // update the label with our dynamic fact
Sina Maleki
Sina Maleki
6,135 Points

did you initialize the fact variable with empty blank first like below? fact = ""; Cheers,