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

Accessing Views in Code

I am working on this video in getting started with android and when I get to this part of the code in MainActivity

//Declare our View variables TextView answerLabel = findViewById(R.id.TextView1); Button getAnswerButton;

I can't get it to work... when in the video is says to do (R.id.) and then it will call up the option to add TextView1, it doesn't, so I add it manually and it doesn't work.

TextView1 can not be resolved or is not a field

is the error I get when I hover over the error marker.

3 Answers

Very quickly!

You wrote TextView answerLabel = findViewById(R.id.TextView1);

The method findViewById() returns a view, but you are selecting a TextView, so you must cast it to a TextView. Something like this...

TextView answerLabel = (TextView) findViewByID(R.id.TextView1);

It seems like there may be a problem with your R.java file.

You can do a project clean that should help you.

Last resort is to delete the Rename the R.java file so a new one will be built and then delete the one that you renamed.

william kenny
william kenny
5,428 Points

I had this problem as well. I deleted the line at the top that says import android.R and that fixed it. Not sure why...

You usually don't need android.R and it will actually cause things to break.

http://stackoverflow.com/questions/15309941/import-android-r-in-eclipse-why