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

Need Help on own App

hey I am working on my own app and for some reason when I start the app on the emulator, after I click the buttons once, they stop working.

here is the code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_twelveiinaf);
    Random randomGenerator = new Random();
    final int randomNumber = randomGenerator.nextInt(51);


    final TextView textViewQuest = (TextView) findViewById(R.id.textViewQuest);
    Button button = (Button) findViewById(R.id.button);
    final View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String fact = "";





            if (randomNumber== 0) {
                fact = "7 W. of the W.";
            }
            else if (randomNumber== 1) {
                fact = "52 C. in a D.";
            }
            else if (randomNumber== 2) {
                fact = "12 M. in a Y.";
            }
            else if (randomNumber== 3) {
                fact = "24 H. in a D.";
            }
            else if (randomNumber== 4) {
                fact = "1085 D. in 3 Y.";
            }
            else if (randomNumber== 5) {
                fact = "64 S. on a C.";
            }
            else if (randomNumber== 6) {
                fact = "12 N. on a C.";
            }
            else if (randomNumber== 7) {
                fact = "60 M. in an H.";
            }
            else if (randomNumber== 8) {
                fact = "4 W. on a C.";
            }
            else if (randomNumber== 9) {
                fact = "26 L. of the A.";
            }
            else if (randomNumber== 10) {
                fact = "1,000 Y. in a M.";
            }
            else if (randomNumber== 11) {
                fact = "1,001 A. N.";
            }
            else if (randomNumber== 12) {
                fact = "12 S. of the Z.";
            }
            else if (randomNumber== 13) {
                fact = "8 P. in the S.S.";
            }
            else if (randomNumber== 14) {
                fact = "8 S. on a S.S.";
            }
            else if (randomNumber== 15) {
                fact = "88 P. K.";
            }
            else if (randomNumber== 16) {
                fact = "13 S. on the A. F.";
            }
            else if (randomNumber== 17) {
                fact = "1 H.C.B. for 1 P.";
            }
            else if (randomNumber== 18) {
                fact = "90 D. in a R. A.";
            }
            else if (randomNumber== 19) {
                fact = "1 T. on a D.";
            }
            else if (randomNumber== 20) {
                fact = "18 H. on a G. C.";
        }
            else if (randomNumber== 21) {
                fact = "3 S. on a T.";
            }
            else if (randomNumber== 22) {
                fact = "5 T. of G.";
            }
            else if (randomNumber== 23) {
                fact = "32 P. to play C.";
            }
            else if (randomNumber== 24) {
                fact = "0 S. in a C.";
            }
            else if (randomNumber== 25) {
                fact = "11 P. on a S. T.";
            }
            else if (randomNumber== 26) {
                fact = "10 D. in a D.";
            }
            else if (randomNumber== 27) {
                fact = "28 D. in F.";
            }
            else if (randomNumber== 28) {
                fact = "2,000 P. in a T.";
            }
            else if (randomNumber== 29) {
                fact = "36 I. in a Y.";
            }
            else if (randomNumber== 30) {
                fact = "2 E. on a P.";
            }
            else if (randomNumber== 31) {
                fact = "101 D.";
            }
            else if (randomNumber== 32) {
                fact = "57 H.V.";
            }
            else if (randomNumber== 33) {
                fact = "8 L. on a S.";
            }
            else if (randomNumber== 34) {
                fact = "5 T. on a F.";
            }
            else if (randomNumber== 35) {
                fact = "9 L. of a C.";
            }
            else if (randomNumber== 36) {
                fact = "66 B. of the B.";
            }
            else if (randomNumber== 37) {
                fact = "2 W. on a B.";
            }
            else if (randomNumber== 38) {
                fact = "4 W. in a M.";
            }
            else if (randomNumber== 39) {
                fact = "8 T. on a O.";
            }
            else if (randomNumber== 40) {
                fact = "4 L. on a C.";
            }
            else if (randomNumber== 41) {
                fact = "13 is an U.N.";
            }
            else if (randomNumber== 42) {
                fact = "50 S. in the U.S.";
            }
            else if (randomNumber== 43) {
                fact = "5 P. on a B.B.T.";
            }
            else if (randomNumber== 44) {
                fact = "1 H. on a U.";
            }
            else if (randomNumber== 45) {
                fact = "2 P. in a F.";
            }
            else if (randomNumber== 46) {
                fact = "4,200 R.";
            }
            else if (randomNumber== 47) {
                fact = "6,000 M. in a R.L.";
            }
            else if (randomNumber== 48) {
                fact = "32 D.F. at which W.F.";
            }
            else if (randomNumber== 49) {
                fact = "100 Y. in a C.";
            }
            else if (randomNumber== 50) {
                fact = "50 Q. in this A.";
            }
            else {
                fact = "Sorry, there was an error! Please try again later.";
            }
            textViewQuest.setText(fact);
        }
    };

    button.setOnClickListener(listener);



    final TextView textViewAnswer = (TextView) findViewById(R.id.textViewAnswer);
    Button button2 = (Button) findViewById(R.id.button2);
    final View.OnClickListener listener1 = new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String fact = "";



            if (randomNumber== 0) {
                fact = "7 WONDERS of the WORLD";
            }
           else if (randomNumber== 1) {
                fact = "52 CARDS in a DECK";
            }
            else if (randomNumber== 2) {
                fact = "12 MONTHS in a YEAR";
            }
            else if (randomNumber== 3) {
                fact = "24 HOURS in a DAY";
            }
            else if (randomNumber== 4) {
                fact = "1085 DAYS in 3 YEARS";
            }
            else if (randomNumber== 5) {
                fact = "64 SQUARES on a CHESSBOARD";
            }
            else if (randomNumber== 6) {
                fact = "12 NUMBERS on a CLOCK";
            }
            else if (randomNumber== 7) {
                fact = "60 MINUTES in a HOUR";
            }
            else if (randomNumber== 8) {
                fact = "4 WHEELS on a CAR";
            }
            else if (randomNumber== 9) {
                fact = "26 LETTERS of the ALPHABET";
            }
            else if (randomNumber== 10) {
                fact = "1,000 YEARS in a MILLENIUM";
            }
            else if (randomNumber== 11) {
                fact = "1,001 ARABIAN NIGHTS";
            }
            else if (randomNumber== 12) {
                fact = "12 SIGNS of the ZODIAC";
            }
            else if (randomNumber== 13) {
                fact = "8 PLANETS in the SOLAR SYSTEM";
            }
            else if (randomNumber== 14) {
                fact = "8 SIDES on a STOP SIGN";
            }
            else if (randomNumber== 15) {
                fact = "88 PIANO KEYS";
            }
            else if (randomNumber== 16) {
                fact = "13 STRIPES on the AMERICAN FLAG";
            }
            else if (randomNumber== 17) {
                fact = "1 HOT CROSS BUN for 1 PENNY";
            }
            else if (randomNumber== 18) {
                fact = "90 DEGREES in a RIGHT ANGLE";
            }
            else if (randomNumber== 19) {
                fact = "1 TAIL on a DOG";
            }
            else if (randomNumber== 20) {
                fact = "18 HOLES on a GOLF COURSE";
            }
            else if (randomNumber== 21) {
                fact = "3 SIDES on a TRIANGLE";
            }
            else if (randomNumber== 22) {
                fact = "5 TYPES of GOVERNMENT";
            }
            else if (randomNumber== 23) {
                fact = "32 PIECES to play CHESS";
            }
            else if (randomNumber== 24) {
                fact = "0 SIDES in a CIRCLE";
            }
            else if (randomNumber== 25) {
                fact = "11 PLAYERS on a SOCCER TEAM";
            }
            else if (randomNumber== 26) {
                fact = "10 DIMES in a DOLLAR";
            }
            else if (randomNumber== 27) {
                fact = "28 DAYS in FEBRUARY";
            }
            else if (randomNumber== 28) {
                fact = "2,000 POUNDS in a TON";
            }
            else if (randomNumber== 29) {
                fact = "36 INCHES in a YARD";
            }
            else if (randomNumber== 30) {
                fact = "2 EARS on a PERSON";
            }
            else if (randomNumber== 31) {
                fact = "101 DALMATIONS";
            }
            else if (randomNumber== 32) {
                fact = "57 HINES VARIETIES";
            }
            else if (randomNumber== 33) {
                fact = "8 LEGS on a SPIDER";
            }
            else if (randomNumber== 34) {
                fact = "5 TOES on a FOOT";
            }
            else if (randomNumber== 35) {
                fact = "9 LIVES of a CAT";
            }
            else if (randomNumber== 36) {
                fact = "66 BOOKS of the BIBLE";
            }
            else if (randomNumber== 37) {
                fact = "2 WINGS on a BAT";
            }
            else if (randomNumber== 38) {
                fact = "4 WEEKS in a MONTH";
            }
            else if (randomNumber== 39) {
                fact = "8 TENTACLES on a OCTOPUS";
            }
            else if (randomNumber== 40) {
                fact = "4 LEGS on a CAT";
            }
            else if (randomNumber== 41) {
                fact = "13 is an UNLUCKY NUMBER";
            }
            else if (randomNumber== 42) {
                fact = "50 STATES in the UNITED STATES";
            }
            else if (randomNumber== 43) {
                fact = "5 PLAYERS on a BASKET BALL TEAM";
            }
            else if (randomNumber== 44) {
                fact = "1 HORN on a UNICORN";
            }
            else if (randomNumber== 45) {
                fact = "2 PARENTS in a FAMILY";
            }
            else if (randomNumber== 46) {
                fact = "4,200 RELIGIONS";
            }
            else if (randomNumber== 47) {
                fact = "6,000 MEN in a ROMAN LEGION";
            }
            else if (randomNumber== 48) {
                fact = "32 DEGREES FAHRENHEIT at which WATER FREEZES";
            }
            else if (randomNumber== 49) {
                fact = "100 YEARS in a CENTURY";
            }
            else if (randomNumber== 50) {
                fact = "50 QUESTIONS in this APP";
            }
            else {
                fact = "Sorry, there was an error! Please try again later.";
            }
            textViewAnswer.setText(fact);
        }
    };

    button2.setOnClickListener(listener1);

what is wrong?

3 Answers

Daniel Hartin
Daniel Hartin
18,106 Points

Hi Akila

In both onClickListener() method calls you create the variable fact which will cause an error, you should instead create a member variable in one place and refer to it in both onClickListener() methods.

Also this code is bit crowded, instead of the massively long if, else if, else statement I would suggest working through the fun facts app course an use a string array and getting the string using the array position. Hard coding values is always a bad idea as it doesn't allow f or change easily. If you work through the fun facts app to completion you will see the workaround to this problem with what Ben does!.

Hope this helps

Daniel

Let me first give you what exactly is happening in your current code.

First you initialise a random class then generate a random number from it. So the first time when the app launches it generates a random number say 25 and stores that within randomNumber variable. Great!

Now when you press the button say ** button2** it uses the same random number 25 already created above. However, the next time you hit the button, it still uses the number 25, since its defined outside the scope of setOnClickListener. So each time condition if(randomNumber == 25) is validated and you see the same answer in its respective label., every time you click the button

Simple solution

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_twelveiinaf);
    Random randomGenerator = new Random();
    final int randomNumber; // just declare don't generate numbers here

final TextView textViewQuest = (TextView) findViewById(R.id.textViewQuest);
    Button button = (Button) findViewById(R.id.button);
    final View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String fact = "";
            randomNumber = randomGenerator.nextInt(51); // move this here and generate randomNumbers here  
// code below

So just declare the randomNumber variable above but don't generate yet. Generate within the onClick method of listener. So now each time you click the button the label will update on run time. Hope I made sense :)

Also with due respect to Daniel, I don't think the fact variable will cause error since they are defined in their own respective scope. Each button will call its respective fact variable. But I second with him not to hard code the values

Daniel Hartin
Daniel Hartin
18,106 Points

Yep! sorry my bad!

the 2 fact variables are in separate method calls oops! not quite sure what I was thinking. Good spot!

Hey Gunjeet.

My code is giving me an error saying that my randomGenerator needs to be declared final. But when I do that, the code gives another error saying that the randomNumber cannot assign a value to a final variable.

What should I do?

Where have you declared the Random variable?

Declare it as I have in my sample code above inside onCreate. In that case you don't have to declare it as final and eventually you wont get the second error

I have put it here

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_twelveiinaf);
        final Random randomGenerator = new Random();
        final int randomNumber; // just declare don't generate numbers here

        final TextView textViewQuest = (TextView) findViewById(R.id.textViewQuest);
        Button button = (Button) findViewById(R.id.button);
        final View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String fact = "";
                randomNumber = randomGenerator.nextInt(51); // move this here and generate randomNumbers here
// code below
                if (randomNumber== 0) {
                    fact = "7 W. of the W.";
                }
                else if (randomNumber== 1) {
                    fact = "52 C. in a D.";
                }
                else if (randomNumber== 2) {
                    fact = "12 M. in a Y.";
                }
                else if (randomNumber== 3) {
                    fact = "24 H. in a D.";

The problem is when you declare something final it's value cannot be changed. Also you will have to initialise final with a value.

So if I say (my mistake too, I should have seen this when you first posted the question)

final int randomNumber;  

Now the variable randomNumber is a constant for the obvious reason it's value should not change.

But this is what we don't want. RandomNumber as we want should keep changing value so that we different result (or fact) each time.

So remove the final from final Random randomGenerator , final int randomNumber; and also from final View.OnClickListener listener and try again. If there are any errors now then let us know

this is how it should look

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_twelveiinaf);
        Random randomGenerator = new Random();
        int randomNumber; // just declare don't generate numbers here

        final TextView textViewQuest = (TextView) findViewById(R.id.textViewQuest);
        Button button = (Button) findViewById(R.id.button);
        View.OnClickListener listener = new View.OnClickListener() {
//code below