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 trialchelseahansell
1,795 PointsAndroid development: unexpected token, unknown class, identifier expected, cannot resolve symbol, etc.
I have 23 errors as I'm trying to add an array to my java in the Introduction to Arrays lesson. I can't see what changed and I tried erasing the changes I made. I also tried cleaning the project and re-building. Nothing has worked.
Help please!
The issues appear to start in the if else statement section, but it also says "cannot resolve listener."
if(randomNumber==0) { fact = "Ants stretch when they wake up in the morning."; }
else if(randomNumber==1)
{
fact = "Ostriches can run faster than horses.";
}
else if(randomNumber==2)
{
fact = "Olympic gold medals are actually made mostly of silver.";
}
else
{
fact = "Sorry, there was an error!";
}
//Update the label with our dynamic fact
factLabel.setText(fact);
}
};
showFactButton.setOnClickListener(listener);
}
miguelcastro2
Courses Plus Student 6,573 PointsFrom what I see thus far you have not defined the event listener. Have you defined it elsewhere?
You can define an anonymous listener like this:
showFactButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Your code goes here.
}
});
4 Answers
Daniel Hartin
18,106 PointsHi Chelsea
I've spotted a couple of things although not sure if it causing all the errors so you will have to make the changes below and comment back if you are still having problems.
- You are missing the ) at the end of you onCllick() method.
- The entire if statement is not necessary as you should now be getting facts from your facts array.
-The closure of your onClick() methos is above the String fact = "" line when it should encapsulate all you code apart from the last line showFactButton.setOnClickListener(listener);
I have posted the corrected code below
public class FunFactsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun_facts);
//Declare 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) {
String[] facts = {
"Ants stretch when they wake up in the morning.",
"Ostriches can run faster than horses.",
"Olympic gold medals are actually made mostly of silver.",
"You are born with 300 bones; by the time you are an adult you will have 206.",
"It takes about 8 minutes for light from the Sun to reach Earth.",
"Some bamboo plants can grow almost a meter in just one day.",
"The state of Florida is bigger than England.",
"Some penguins can leap 2-3 meters out of the water.",
"On average, it takes 66 days to form a new habit.",
"Mammoths still walked the earth when the Great Pyramid was being built."};
//The button was clicked, so update the fact label with a new fact
String fact;
//Randomly select a fact
Random randomGenerator = new Random(); //Construct a new random variable generator
int randomNumber = randomGenerator.nextInt(10);
fact = facts[randomNumber];
//Update the label with our dynamic fact
factLabel.setText(fact);
});
showFactButton.setOnClickListener(listener);
}
Hope this helps
Daniel
chelseahansell
1,795 PointsHere is the entire code. I followed the videos carefully and everything worked until I started adding array data. I'm guessing I either deleted something that broke the rest of the code or forgot a symbol, but I'm too inexperienced to see where it went wrong. Thank you everyone!
public class FunFactsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun_facts);
//Declare 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) {
String[] facts = {
"Ants stretch when they wake up in the morning.",
"Ostriches can run faster than horses.",
"Olympic gold medals are actually made mostly of silver.",
"You are born with 300 bones; by the time you are an adult you will have 206.",
"It takes about 8 minutes for light from the Sun to reach Earth.",
"Some bamboo plants can grow almost a meter in just one day.",
"The state of Florida is bigger than England.",
"Some penguins can leap 2-3 meters out of the water.",
"On average, it takes 66 days to form a new habit.",
"Mammoths still walked the earth when the Great Pyramid was being built."};
}
//The button was clicked, so update the fact label with a new fact
String fact = "";
//Randomly select a fact
Random randomGenerator = new Random(); //Construct a new random variable generator
int randomNumber = randomGenerator.nextInt(10);
if(randomNumber==0)
{
fact = "Ants stretch when they wake up in the morning.";
}
else if(randomNumber==1)
{
fact = "Ostriches can run faster than horses.";
}
else if(randomNumber==2)
{
fact = "Olympic gold medals are actually made mostly of silver.";
}
else
{
fact = "Sorry, there was an error!";
}
//Update the label with our dynamic fact
factLabel.setText(fact);
};
showFactButton.setOnClickListener(listener);
}
Steven Byrne
4,120 PointsYour indentation makes me think that your "if" code is not in the onClick method. The variable decls are not causing errors because they are member variables in the anon inner class. Perhaps you want to move the closing brace here
"Mammoths still walked the earth when the Great Pyramid was being built."};
} <--------------------
// line numbers in the example would help -- don't know if that's an option
down to below your executable code.
chelseahansell
1,795 PointsIt turned out to be a } in the wrong place. I'm still not sure how it got out of place, but I moved it all over with no luck. Then I went back and examined the code from the video line by line. It's in the right place now.
Thanks guys!
Daniel Hartin
18,106 PointsDaniel Hartin
18,106 PointsHi chelsea,
Can't see anything wrong with the code you have posted. Can you post the entire code please. I suspect something went wrong when you set you listener variable to a new onClickListener, but I can't tell without seeing the entire code
Thanks Daniel