Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android

Not getting RESPONSES with Crystal Ball

Emulator runs fine but when I click on 'Enlighten Me' I don't receive the numbers 0,1, or 2 and now I haven't gotten the yes, no, or maybe. Eclipse hasn't given me an error message either. Could someone explain what I may be doing wrong?

package com.example.crystal.ball;

import java.util.Random;

import android.os.Bundle;

import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.TextView;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Declare our View variables and assign them the View from the layout file

    final TextView answerLabel = (TextView) findViewById(R.id.textView1);
    Button getAnswerButton = (Button) findViewById(R.id.button1);

    getAnswerButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // The button was clicked, so update the answer label with an answer
            String answer = "";

            // Randomly select one of three answers: yes, no, maybe
            Random randomGenerator = new Random();

            // Construct a new Random generator
            int randomNumber = randomGenerator.nextInt(3);


            /* Convert the randomNumber to a text answer
             * 0 = Yes
             * 1 = No
             * 2 = Maybe
             */

            if (randomNumber == 0) {
                answer = "Yes";
            }   
            else if (randomNumber == 1) {
                answer = "No";
            }
            else if (randomNumber == 2) {
                answer = "Maybe";   
            }
            else {
                answer = "Sorry, there was a mistake!!";
            }

            // Update the label with our dynamic answer

            answerLabel.setText(answer);
        }
    });
}

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

}

1 Answer

Hi Allen, are the ID's for your button and textview "button1" and "textView1". Make sure that these are the correct ID's.

Based your description above, this might be the cause of your problem.