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)

The random numbers won't appear when I click the button

When I open the emulator and press the "Another Fun Fact" button the text does not change and stays as "Ant's stretch when they wake up in the morning." Here is my code below:

package com.dwayne.funfacts;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;

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 them the views from the layout file
        final TextView factLabel = (TextView) findViewById(R.id.txtFactView);
        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 a fact
                Random randomGenerator = new Random(); //Construct a new Random number generator
                int randomNumber = randomGenerator.nextInt(3);
                fact = randomNumber + "";
                factLabel.setText(fact);
            }
        };
    showFactButton.setOnClickListener(Listener);
    }
}

Check the line "final TextView factLabel = (TextView) findViewById(R.id.txtFactView);".

I think that the findViewById method should use the parameter "R.id.factTextView" instead of "R.id.txtFactView" to correspond to the TextView in the layout.

Hi Nicholas, Thanks for the help but when I try it has an error.

I checked the .xml file and my id is as follows for the text: android:id="@+txtFactView"

Do you think there is a problem here?