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 trialEdu Pucheta
286 PointsMy code is in read,and i donΒ΄t know why.
public class FunFactsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun_facts);
//Declare our view variables and assign 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) {
// the button was clicked so a new fact is displayed
String fact = "Ostrages can run faster than horses" ;
factLabel.setText(fact);
};
};
showFactButton.setOnClickListener(listener);
}
The second time that says showFactButton is in red. :(
2 Answers
Daniel Hartin
18,106 PointsHi Edu
Not entirely sure I know what you mean, however from looking at the code you've posted the second showFactButton is here in the line findViewById(R.id.showFactButton);
If this showFactButton is underlined in red it is highly likely your XML file has a different id set for the Button element and may just be a simple typo error.
Go into your XML layout file and look for the line
android:id="@+id/showFactButton"
Please bear in mind the line that declared the id must be spelt exactly the same as it is in your java code.
Hope this helps, but if it doesn't solve it or your having problems just paste your XML code back here
Daniel
Edu Pucheta
286 PointsThanks you! Really accurate and quick reply!
I think i solved that problem. Now, i do not know what happens. The button just does not work.
public class FunFactsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun_facts);
//Declare our view variables and assign 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) {
// the button was clicked so a new fact is displayed
String fact = "Ostriches can run faster than horses.";
factLabel.setText(fact);
}
};
showFactButton.setOnClickListener(listener);
}