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) 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