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

ibrahim dirir
ibrahim dirir
11,290 Points

challenge Task 1 of 1

task 1 of 1

MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

    protected Button mExterminateButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
      mExterminateButton exterminate =  new mExterminateButton
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Declare our view variables

    }

}

1 Answer

Alex Johnson
Alex Johnson
6,067 Points

As the challenge says, the mExterminateButton has already been declared, you just need to initialize it. There are a few parts to this. First, you need to use the method findViewById() and pass in the ID they give you in the prompt (button1). But you can't just use button1, you have to say where it is (by using R.id.). Finally, you need to cast the object that's returned as a Button, since that's what mExterminateButton is expecting.

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

    // Button declared
    protected Button mExterminateButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initialize mExterminateButton by finding view by ID and cast as a Button
        mExterminateButton = (Button) findViewById(R.id.button1);
    }
}