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 trialSonya Cook
1,307 PointsHelp needed: Challenge Task 1
Having problems with this challenge
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
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
}
}
1 Answer
Christopher Augg
21,223 PointsHello Sonya,
I always like to break down the instructions to get a good understanding of what I need to do for a 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!
- Initialize mExterminateButton using findViewById() method.
- The id for the button is button1. We need to pass R.id.button1 into the findViewById() method.
- We need to cast the generic view we get from calling the findViewById() to a Button in order to assign it to mExterminateButton.
mExterminateButton = (Button) findViewById(R.id.button1);
As you can see, we are calling the findViewById method and passing it R.id.button1. This returns a generic view based on that id. Therefore, we cast it using (Button) to assign it to mExterminateButton.
Remember, the R class is dynamically generated during the build process. It provides a link to the resources like Strings, android widgets, graphics, and layouts. You can check this out to get a better understanding.
Hope this helps.
Regards,
Chris