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 trialQuantavius Mills
491 PointsErrors at the bottom of code.
I am getting a few errors at the bottom of the code. public class MainActivity<TextVie> extends AppCompatActivity { //Declare our view variables private TextView factTextView; private Button showFactButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Assign the Viewid from the layout file to the corresponding variables
factTextView = findViewById(R.id.factTextView);
showFactButton = findViewById(R. id.showFactButton);
String[] facts;
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.",
"You are born with 300 bones; by the time you are an adult you will have 206.",
"Olympic gold medals are actually made mostly of silver.",
"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 TextView with a new fact
// Randomly select a fact
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(10);
String fact = facts[randomNumber];
// Update the screen with our new fact
factTextView.setText(fact); <--- cannot resolve factTextView, (fact) unknown class
};
};
showFactButton.setOnClickListener(listener); <--- cannot resolve
}
}
Quantavius Mills
491 PointsHi Mohammed,
Thanks for answering, I removed the semicolon for the lines and it just created more errors. I am still researching this issue.
1 Answer
Fatemah Tavakoli
13,797 PointsHi Quantavius,
Your code is fine, you just have an extra "}" in middle of your code that is causing the error. Apart from the "showFactButton.setOnClickListener(listener);" the rest of the codes are part of the View.OnClickListener, which you are closing it after your facts array with that extra "}". Thus, the rest of codes became out of the scope.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Assign the Viewid from the layout file to the corresponding variables
factTextView = findViewById(R.id.factTextView);
showFactButton = findViewById(R. id.showFactButton);
String[] facts;
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.",
"You are born with 300 bones; by the time you are an adult you will have 206.",
"Olympic gold medals are actually made mostly of silver.",
"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 TextView with a new fact
// Randomly select a fact
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(10);
String fact = facts[randomNumber];
// Update the screen with our new fact
factTextView.setText(fact); //<--- cannot resolve factTextView, (fact) unknown class
};
};
showFactButton.setOnClickListener(listener); //<--- cannot resolve
}
}
Keep up the good work! :D
Quantavius Mills
491 PointsThanks Fatima!! I appreciate your feedback :)
Mohammad Algarni
924 PointsMohammad Algarni
924 PointsHi,
Remove the semicolon from the end of the line as shown in the screenshot below and try to run the app again: