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!
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
Chip Lay
964 PointsrandomGenerator
I've been following the lessons, but it seems like I've either skipped something or missed something along the way.
I get an yellow line underneath randomGenerator. Which basically means the value of the local variable is not being used. I can't figure out to make to make it be used?
Here's my code:
package com.example.crystalball;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
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 Views from the layout file
final TextView answerLabel = (TextView) findViewById(R.id.textView1);
Button getAnswerButton = (Button) findViewById(R.id.button1);
getAnswerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
String[] answers = {
"Let Me Think About it." ,
"I dont know if I can answer that!" ,
"What's the problem? Can't think it yourself?" ,
"Just go and do it!" ,
"Probably Not!" ,
"You got it!"
};
// The button was clicked, so update the answer label with an answer
String answer = "";
// Randomly select one of three answers: Yes, No, or Maybe
Random randomGenerator = new Random();
int randomNumber = randomGenerator(answers.length);
if (randomNumber == 0) {
answer = "Yes";
}
else if (randomNumber == 1) {
answer = "No";
}
else if (randomNumber == 2) {
answer = "Maybe";
}
else {
answer = "Sorry!";
}
// Update the label with our dynamic answer
answerLabel.setText(answer);
}
});
}
protected int randomGenerator(int i) {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Thanks!
2 Answers

Ben Jakuben
Treehouse TeacherLooking pretty good! It's an easy thing to miss because even with a close look everything appears to be in place. But check the line where you set randomNumber. You need to call the nextInt() method--I'm surprised that line isn't showing an error in Eclipse!

Chip Lay
964 PointsAhh yes.
It actually showed me the error once I reached the refactoring section.
I see it. Thanks for the help!