Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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: