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) Improving Our Code Simple Refactoring: Creating a Class

David Bogdanov
David Bogdanov
1,120 Points

No_fact_is_showing_up

here_is_my_code: package david.funfacts;

import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.Button; import android.widget.TextView;

import java.util.Random;

public class FunFactsActivity extends AppCompatActivity {

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


    //declare our view variables and assing them the vievs from the layout file.
    final TextView factLabel = (TextView) findViewById(R.id.factLabel);
    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 to show something new


            factLabel.setText("");
        }  // end of onClick()
    };  // end of View.OnClickListener

    showFactButton.setOnClickListener(listener);
}

} i_can't_see_any_fact_when_i_press_the_button

package david.funfacts;

import java.util.Random;

/**

  • Created by david on 07-04-2016. */ public class FactBook {

    public String getFact() { String[] facts = { "Ants stretch when they wake up in the morning.", "Ostriches can run faster than horses.", "Olympic gold medals are actually made mostly of silver (about 90%).", "You are born with 300 bones; by the time you are an adult you will have 206.", "It takes about 8 minutes for light from the Sun to reach Earth.", "Some bamboo plants can grow almost a meter in just one day.", "The state of Florida is bigger than England.", "Some penguins can leap 2-3 meters out of the water.", "On average, it takes 66 days to form a new habit.", "Mammoths still walked the earth when the Great Pyramid was being built." };

    String fact = "";
    
    //randomly sellect a fact
    Random randomGenerator = new Random(); //construct a new random number generator
    int randomNumber = randomGenerator.nextInt(facts.length);
    
    fact = facts[randomNumber];
    
    return fact;
    

    }

}

1 Answer

anil rahman
anil rahman
7,786 Points
@Override
        public void onClick(View v) {
        // the button was clicked, so update the fact to show something new


            factLabel.setText("");
        }  // end of onClick()

In the on click method you are saying you want to see anew fact, but if you notice in the .setText() its et to empty string as in show nothing, those "" need to be changed to the getFact Method.