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) Coding the Fun Facts Using Conditionals (if Statements)

Once I added the random fact text, the setOnclickListener and other things broke below

Several items are showing red and asking for me to change things that are exactly like Ben's.

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

I have tried to compare line to line to the lesson and then started trying to fix it, so I might have made it worse.

Help!

P.S. When answering please remember that I am extreme novice

Usually when you add something new to your code and the other things go red, the thing you added is the issue. Can you link to us the code you added. I see this one is not where you add the random fact.

4 Answers

public void onClick(View view) { //The buttons was clicked so update the fact label with a new fact String fact = ""; //Randomly select a fact Random randomGenerator = new Random();//Construct a new Random number generator int randomNumber = randomGenerator.nextInt(3);

            /*Convert the randomNumber to a text fact
            * 0 = “Corgis are the favorite dog of Queen Elizabeth.”
            * 1 = “Corgis excel in obedience, agility and flyball.”
            * 2 = “There are two types of corgis: Cardigan and Pembroke.”
            */


            if (randomNumber == 0){
                fact = "Corgis are the favorite dog of Queen Elizabeth";
             //Update the label with our dynamic fact
             factLabel.setText(fact);
        }
    };
    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.corgi_fun_facts, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);

I don't really see an error in your code. Can you hover your mouse on the red places and see what error messages they note?

Yes, but I don't understand what it wants me to do. The code under the randomNumber was fine and then it wasn't even though I did not change anything. That is what is really stressing me out. I tried fixing the code based on hovering over the error and it created more problems.

I don't really see an error, have you added corgi_fun_facts in the Strings file?

Guilherme Mendonca
Guilherme Mendonca
3,064 Points

Hello Kimberly,

It seems to me that you forgot to close brackets after the IF statement.

          if (randomNumber == 0){
              fact = "Corgis are the favorite dog of Queen Elizabeth";

           -----> HERE <--------       

         factLabel.setText(fact);
    }

}; ........................................

Hope I helped!

william parrish
william parrish
13,774 Points

showFactButton.setOnClickListener(listener);

Where is "listener" defined, and where did you implement the logic you want to execute when things are actually clicked.

Usually an on click listener looks something more like:

```.setOnClickListner( new View.onClickListener){

```  public void onClick(View v) {

// this is where you actually type what you want to happen when its clicked }