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 User Input Finding Views by IDs

Why there are no clues to help knowing the steps we should take?! I'm stuck and I can't proceed.

I have no idea what am I doing wrong.

MainActivity.java
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);
      Button exterminateButton = findViewById(R.id.button1);

        // Declare our view variables

    }

}

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Eliran Elgozi! First, I'd like to say that in my opinion, I believe there are clues as to why your line of code isn't working. The first clue is in the task instructions:

Don't forget to cast the generic View returned by the method!

Secondly, if you click the "preview" button you will be shown the compiler errors where you will find this:

./MainActivity.java:13: error: incompatible types: View cannot be converted to Button Button exterminateButton = findViewById(R.id.button1);

What it's saying is this: you've declared a variable of type Button, but findViewById doesn't return a Button, it returns a View. It can't make the conversion from View to Button by itself. It needs your help. You must cast the View to a Button explicitly.

We do this by:

Button exterminateButton = (Button)findViewById(R.id.button1);

Hope this clears things up! :sparkles: