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

What seems to be the problem?

// Declare our View variables and assign them 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 view) {
              // The button was clicked so update the fact label with a new fact.
                String fact = "Salsa e mai rapida decat bachata.";
                factLabel.setText(fact);
            }
        };
        showFactButton.setOnClickListener(listener);
    }

It doesn't recognize factTextView and showFactButton

David Anton
David Anton
Courses Plus Student 30,936 Points

Hi Razvan,

Try to add factLabel as global variable like class member. declare mFactLabel before "onCreate" method. and use it in the onClick method

1 Answer

Daniel Hartin
Daniel Hartin
18,106 Points

Hi Razvan

Have you got your XML layout file you could paste in please. Your code looks fine to me, the problem must lie with a difference in the id's (I suspect the name is slightly different in your XML file) if you've checked this and it's all okay try cleaning and rebuilding the project. Sometimes it likes to throw a wobbly with R.

Thanks Daniel