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 a Simple Android App (2014) Basic Android Programming Initializing a Button

How to initialize the showFactButton variable using the findViewById() method.

Android Development Challenge task 1 of 1 How to initialize the showFactButton variable using the findViewById() method like the TextView above it. The ID for the button is showFactButton, and don't forget to cast it to a Button with (Button)!

Here is what I wrote:

    // Declare our View variables and assign them the Views from the layout file

TextView factLabel = (TextView) findViewById(R.id.factTextView); Button showFactButton= findViewById(R.id.showFactButton);

But there is one error in output.html ./FunFactsActivity.java:16: incompatible types found : android.view.View required: android.widget.Button Button showFactButton= findViewById (R.id.showFactButton); ^ 1 error Any suggestion to solve this 1 error? Thanks in advance.

4 Answers

Tom Mertz
Tom Mertz
15,254 Points

Hey Beau,

It looks like you forgot to typecast your button. Try adding (Button) before your findViewById of your fact button.

Let me know if you get it working.

Hi Tom, Thanks in a million times. It worked and I really appreciate your help.

Tom Mertz
Tom Mertz
15,254 Points

No problem, glad you got it working!

Hi Beau,

You might want to add

```.java

before your code and three more backticks after it. (a backtick is not a ' but a `. they are usually found under a ~ on a keyboard.)

Button showFactButton = (Button) findViewById(R.id.showFactButton);

./FunFactsActivity.java:17: error: variable factLabel is already defined in method onCreate(Bundle) TextView factLabel = (TextView) findViewById(R.id.factTextView); ^ ./FunFactsActivity.java:18: error: variable showFactButton is already defined in method onCreate(Bundle) Button showFactButton; ^ 2 errors

How to solve this error?