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 trialFarai Ted Mandoreba
21,518 PointsInitialize the showFactButton variable using the findViewById() method like the TextView above it. The ID for the button
what is the correct answer mine is wrong, and is pointing at shawfactbutton pointing at the "s"
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class FunFactsActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun_facts);
Button showFactButton = (Button) findViewById(R.id.showFactButton);
TextView factLabel = (TextView) findViewById(R.id.factTextView);
Button showFactButton;
}
}
1 Answer
Lauren Moineau
9,483 PointsHi Farai. The solution for this challenge was posted in your other post.
But let's have a look at the problem here, just so you know. When clicking on Preview
, you see:
./FunFactsActivity.java:18: error: variable showFactButton is already defined in method onCreate(Bundle)
Button showFactButton;
^
1 error
It says "error: variable showFactButton is already defined in method onCreate(Bundle)"
If you look at the code, you will see that Button showFactButton
is indeed defined twice in the code:
Button showFactButton = (Button) findViewById(R.id.showFactButton); // Defined here
TextView factLabel = (TextView) findViewById(R.id.factTextView);
Button showFactButton; // Defined here
Just remove the last line (Button showFactButton;
) and you'll be fine.
Hope that helps :)
Farai Ted Mandoreba
21,518 PointsFarai Ted Mandoreba
21,518 Pointsthanks a lot it helped, others will actually add more confusion