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

App crashes when running the fun facts program on simulator.

I'm new to android so I followed the instructions given in the video. I reached the part where I add the fun facts to an array and run the program. I get the first fun fact screen, but when I tap on the button, the app crashes. What am I doing wrong? See code below

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;

public class FunFactsActivity extends AppCompatActivity {

    private TextView factTextView;
    private Button showFactButton;


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

        factTextView = (TextView) findViewById(R.id.factTextView);
        showFactButton = (Button) findViewById(R.id.showFactButton);

        View.OnClickListener listener = new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                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.",
                        "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 = "";

                Random randomGenerator = new Random();
                int randomNumber = randomGenerator.nextInt(fact.length());
                fact = facts[randomNumber];

                factTextView.setText(fact);

            }
        };
        showFactButton.setOnClickListener(listener);





    }
}

nelson cordero can you post a stack trace (red text that shows up in Android Studio when a crash occurs)?

1 Answer

Just quickly glancing at your code, I see at least 1 logic error:

int randomNumber = randomGenerator.nextInt(fact.length());
//                                         ^
// "fact.length()" should be facts.length since you want it to pick a
// random element from the facts array.
// fact.length() gets you the length of the string which is currently equal to 0.
// which feeds into the randomGenerator giving you a random number between
// 0 to 0 which will yield 0.