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 Adding the onClick() Method

the showFactButton is highlighted red an wont let me run app need help, why is it red?

even when i copy an paste the code from the site its still red need help

it says cannot resolve symbol

3 Answers

Dhruv Gupta
Dhruv Gupta
4,320 Points

Two potential things,

You haven't initialized a button like this. private Button showFactButton;

Or you haven't found the id

showFactButton = (Button) findViewbyId(R.id.showfactbutton);

Apart from that my guess is as good as yours

thanks you helped me figure it out, i appreciate it

Angel Sosa
Angel Sosa
892 Points

Hello guys, Im having this issue(.setOnClickListener is showing in red):

public class FunFactsActivity extends Activity {

@Override
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 file.
final TextView factLabel = (TextView) findViewById(R.id.factTextView);
Button showFactButton = (Button) findViewById(R.id.showFactButton);
View.OnClickListener listener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //The button was clicked, so update the fact label with new fact
        String fact = "Cristo es que salva!";
        factLabel.setText(fact);

    }
};
showFactButton.setOnClickListener(listener);

}