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

Android Build an Interactive Story App (Retired) User Input Finding Views by IDs

Anthony Ramirez
Anthony Ramirez
1,014 Points

I believe I don't understand the question, I have tried final Button button1 = (Button) findViewById(R.i.button1)

I have tried Whatever I think Is on my repertoire, maybe I am missing some logic: I's tried final Button button1 = (Button) findViewById(R.id.button1); I also tried final Button mExterminateButton = (Button) findViewById(R.id.button1); Still not getting an proper answer! please help me understand. Thanks Ahead

1 Answer

Seth Kroger
Seth Kroger
56,414 Points

First, where are you typing the code? Finding Views by id can't happen until you call setContentView() in onCreate() to attach the layout to the Activity. That means you have to be writing your code after that.

Second, the variable mExterminateButton is already declared as a member variable, so you don't need the type declaration again. The code you need to write should be mExterminateButton = (Button) findViewById(R.id.button1)

(Declaring the type again will create a separate local variable called mExterminateButton that will supersede the member variable, but will disappear after onCreate() is finished. The member variable will be left unset.)