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

Sherry Evans
Sherry Evans
3,377 Points

Hi I have a few problems I can't seem to straighten out

First I can't get the Emulator to work. My R is not recognized anywhere in the document , and randomNumber only works with a lowercase n not a capital N. The video looks like it should be a capital? Help! cant fix! Ty

Stone Preston
Stone Preston
42,016 Points

can you post your code

Sherry Evans
Sherry Evans
3,377 Points

I don't know how to post it from android studio. Any ideas? I tried to get it into Code Pen. Sorry I'm a newbie to apps. :(

Stone Preston
Stone Preston
42,016 Points

just copy and paste from android studio into the forum text box. post all the code in the class thats showing the error

Sherry Evans
Sherry Evans
3,377 Points

Maybe this will work?

package com. treehouse.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.factTextView);
        Button showFactButton = (Button)findViewById(R.id.showFactButton);
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //The button was clicked to update the fact label with a new fact
                String fact ="" ;
                //Randomly select a fact
                Random randomGenerator = new Random (); // Construct a new random generator
                int randomNumber = randomGenerator.nextInt(3);
                fact = randomNumber + "";

                //Update the label with our dynamic fact
                factLabel.setText(fact);


            }
        };
        showFactButton.setOnClickListener(listener);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.fun_facts, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);

    }
}

1 Answer

Erin Leathers
Erin Leathers
3,562 Points

Thank you Sherry !

I instinctively thought to look at the import line as I had the problems with the others.

Before I delved into the forums though I tried to android.widget.Random : ) because something along that syntax worked previously.

All of this info is overwhelming but I am determined to stick with and hope osmosis will occur !

Anyway thanks so much , and to everyone else if "Random" is showing in red you must import something : )