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 Basic Android Programming Generating a Random Number

Steve McNutt
Steve McNutt
2,562 Points

[SOLVED] - Can someone help me fix this bug in Generating a Random Number?

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

    // Assign the views from layout file to the corresponding variables
    mFactTextView = (TextView) findViewById(R.id.factTextView);
    mShowFactButton = (Button) findViewById(R.id.showFactButton);

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           // update to a new fact after the button has been clicked
            String fact = "";
            // Randomly select a new fact
            Random randomGenerator = new Random();
            int randomNumber = randomGenerator.nextInt(3);
            fact = randomNumber + "";

            // update the screen with our dynamic fact
            mFactTextView.setText(fact);
        }
    };
    mShowFactButton.setOnClickListener(listener);
}

This is the code that I have entered into FunFactsActivity.java. When you run the app on the emulator, it's supposed to make it so that each time you click the button a random number between 0 and 2 appears on the screen. For some reason, when I run the app and click the button, it becomes lateral, moves to the left side of the screen, and loses most of its width. It also does not display any number. I have looked back through other videos and my code matches, and the gradle gives me 0 errors and 0 warnings. I'm not sure if I need to provide more information, but does anyone have any idea why my button keeps re-sizing and relocating?

[SOLVED]

Steve McNutt
Steve McNutt
2,562 Points

Nevermind. I managed to find the error. Back in the XML sheet, I had my button layout:width set to wrap content instead of fill parent.

It turns out that it had nothing to do with the java file.

[SOLVED]