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 trialwang hong
Courses Plus Student 147 Pointshello,how can I solve this problem ?No matter how hard I try ,it can not through ,give me a hand please,thanks.
hello,how can I solve this problem ?No matter how hard I try ,it can not through ,I don't know how to solve this ,give me a hand please,thanks.
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
}
}
2 Answers
Steve Hunter
57,712 PointsLet's do this!
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!
We have mExterminateButton
declared in the class, we to initialize inside onCreate()
. The GUI has a button called button1
that needs linking to this variable in the code. We use findViewById()
to do this, casting the returned object to the required type. That's a Button
in this case.
mExterminateButton = (Button) findViewById(R.id.button1);
That should do it!
Steve.
wang hong
Courses Plus Student 147 Pointsimport 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
mExterminateButton = (Button) findViewById(R.id.button1);
}
}
wang hong
Courses Plus Student 147 Pointswang hong
Courses Plus Student 147 Pointsyes,I also write like yours,but it show me it is wrong!What happened?
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsLet me try that again ... ?!
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsThat code does work:
http://goo.gl/QgG6yh
Can you paste your code, please?
Steve.
wang hong
Courses Plus Student 147 Pointswang hong
Courses Plus Student 147 PointsThanks,the code I have paste there.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsThat's really strange.
I suggest you hit Ctrl+F5 to reload the challenge fully and try that code again - there seems nothing wrong with it!
wang hong
Courses Plus Student 147 Pointswang hong
Courses Plus Student 147 Pointsok,Thank you,if I have problem,I will ask you again.Thank you very much .
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsPlease do, yes - let's got you through this!