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 Making a Button Do Something

Urgent Pls The symbol showFactButton is in Red Letters

showFactButton.setOnClickListener(listener); when i hover above it says cannot resolve symbol 'showFactButton' which mentioned it is in red letters

in gradle build it shows the following errors

Error:(31, 9) error: cannot find symbol variable showFactButton

Error:Execution failed for task ':app:compileDebugJava'. > Compilation failed; see the compiler error output for details.

3 Answers

Harry James
Harry James
14,780 Points

Hey there Nicholas!

Have you declared showFactButton in your code? Also, make sure you are declaring showFactButton before you make the call to set an onClickListener()!

You should have a line of code like this immidiately after setContentView():

final Button showFactButton = (Button) findViewById(R.id.showFactButton);

Don't worry if you don't have the final keyword in front yet! Ben talks about this later on in the course and, also, Android Studio will probably automatically add it later!

If you are still having problems, let me know! :)

but Ben's code for Button does not have final

i've resolved several issues the only problem i have now is with the R

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fun_facts);

        // Declare our View variables and assign the views from 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 = "";
                  // Randomly select fact
                  Random randomGenerator = new Random();
                  int randomNumber = randomGenerator.nextInt(3);
                  fact = randomNumber + "";

                  // Update the label with our dynamic fact
                  factLabel.setText(fact);
            }
        };
        showFactButton.setOnClickListener(listener);


}
Harry James
Harry James
14,780 Points

Hey Nicholas!

What is the problem with the R? Is there are a problem with the R class or is it just that a specific resource can not be found?

If you're not sure, hover over the error and let me know what it says :)

thanks Harry for asking i've managed to fix it.

Harry James
Harry James
14,780 Points

Woohoo! Glad it's all sorted now!

I encourage you to mark a post as the Best Answer so that other forum readers will know that this question has been resolved :)