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 trial
W Joel Baker
1,012 PointsI'm lost with this challenge - Initialize the member variable mExterminateButton using the findViewById() method.
I did some research but can't seem to figure out what I need to be doing.
Challenge Initialize the member variable mExterminateButton using the findViewById() method. The ID for the button is button1. Don't forget to cast the generic View returned by the method!
My code is the int mExterminateButton = (Button) findViewById(R.id.button1); line
public class MainActivity extends Activity {
protected Button mExterminateButton;
int mExterminateButton = (Button) findViewById(R.id.button1);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Declare our view variables
}
}
5 Answers
Steve Hunter
57,712 PointsHi Joel,
You're doing fine - your code is correct, mainly.
As I guessed at before, if you add your line (without the int bit) after the comment in onCreate you're spot on.
So:
public class MainActivity extends Activity {
protected Button mExterminateButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Declare our view variables
mExterminateButton = (Button) findViewById(R.id.button1); // <- your code here
}
}
Sorted!
Steve.
W Joel Baker
1,012 PointsHey Steve, I'm stuck on another challenge. Can you help with this one by chance? https://teamtreehouse.com/forum/how-do-i-use-an-intent-with-the-first-flightactivity-challenge
Steve Hunter
57,712 PointsI shall have a look later and see if I can help.
Steve.
William Dalessi
1,266 PointsAdd the following after // Declare our new variable and it should work:
final Button mExterminateButton = (Button) findViewById(R.id.button1);
I am sorry I do not completely understand it myself, but it builds on the last lesson.
Basically declareing a (Button) with the constant (final) tag. Using the Button name (mExterminateButton) Using findById with properties (R.id.button1)
"button1" is the given button id to use.
Steve Hunter
57,712 PointsHi WIlliam,
In this challenge, the Button instance, mExterminateButton is already declared, so there's no need to redeclare it.
So, omitting your final Button beginning to that line of code but doing everything else as you'd suggested will work fine, as Joel found.
Steve.
W Joel Baker
1,012 PointsHey Steve,
http://teamtreehouse.com/library/build-an-interactive-story-app/user-input/finding-views-by-ids
I do see I have it in the wrong spot. I'm as green as they come and hoping I start to understand what I'm doing soon.
W Joel Baker
1,012 PointsThat did it Steve! Thank you.
Salomon Alcoba Trujillo
Courses Plus Student 321 Pointshi i solved with this code:
before:
like the coment say "declare View Variables" so start here:
View mExterminateButton= findViewById(R.id.button1);
why this works BECAUSE findViewById return a View Variable so you dont need to make a Button View variable.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsHiya,
I edited your post to make the code easier to read.
Can you send me alink to the challenge, please. That way, I'll make sure my suggested code is correct!
My first thought would be to delete the
intword and move the whole line into theonCreatemethod. But I need to check that first within the challenge.Steve.