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

I can't understand why my my button will not work with my random generator no answers.

FunFactsActivity.Java

package com.example.waynegreenwood.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 {

private FactBook mFactBook = new FactBook();

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

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

           String fact = mFactBook.getFact();

            factLabel.setText("fact");
        }
    };
    showFactButton.setOnClickListener(listener);


}


}

The below is my FactBook.java to hold my arrays.

package com.example.waynegreenwood.funfacts;

import java.util.Random;

public class FactBook {

public String[] mFacts = {

        "Ostriches run faster than horses.",
        "Gold medals are actually made more of silver.",
        "Men run faster than women.",
        "Elephants weigh heavier than some small cars.",
        "The giant squid is real.",
        "Bids are a direct descendant of the RAPTOR.",
        "The worlds gold is running out.",
        "Woman can multi-task better than men.",
        "Ufo's are not real."
};
public String getFact(){
    String fact = " ";

    Random randomGenerator = new Random(); // construct a new random operator
    int randomNumber = randomGenerator.nextInt(mFacts.length);
    // if randomNumber equal to 0 then
    fact = mFacts[randomNumber];

    return fact;


}

}

Joseph Greevy
Joseph Greevy
4,990 Points

It look very like you have messed up on naming cause your code seems sound to me. Use the ctrl k and do a lot of boring work trying to look for something that doesn't match as I don't think there is anything fundamentally wrong

2 Answers

Joe Brown
Joe Brown
21,465 Points

When you are calling setText in your onClick you have fact in quotations as "fact", but it is already a string so there is no need for the quotes. Although I would think that would at least set your textView to actually display the word "fact". So maybe it is one of those simple over looked things that happens like Joseph already mentioned

Hi Joe and Joseph

Thank you for the swift reply it is much appreciated, I will go over my code with a fine tooth comb and look for anything that does not match in my code. i previously removed the fact and left just "" and instead of fact appearing as an answer I clicked the button the answer is was blank. The random generator i don't think is selecting an answer after the button is pressed.