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 trialNkosinolwazi Moyo
9,588 PointsFind view by Id
I don't know what's wrong with my coding
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
protected Button exterminateButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Declare our view variables
exterminateButton= (Button)findViewById (R.id.exterminateButton ) ;
}
}
3 Answers
Lauren Moineau
9,483 PointsHi. The instructions say "the ID for the button is button1
" It looks like you put the wrong id there. Correct it and you should be good to go :)
Nkosinolwazi Moyo
9,588 PointsStill don't get it.
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
protected Button exterminateButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Declare our view variables
exterminateButton1= (Button)findViewById (R.id.exterminateButton1 ) ;
[MOD: edited code block]
Lauren Moineau
9,483 PointsYou are not calling the view with the id button1
in your code, but a view with another id. Just make sure you use the correct id, as per the instructions.
If you're still a bit unsure as to what an id is and how to use it, just re-watch the video and follow the same steps here.
Sorry if I don't serve you the answer on a plate here, but I'm a firm believer in the fact that solving it yourself with a little clue will be more helpful to you in the future. I hope you understand :)
Steve Hunter
57,712 Points
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsCan you copy in your XML code too? That's where the
R.id.
is pointing.Thanks,
Steve.