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

Find view by Id

I don't know what's wrong with my coding

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);

        // Declare our view variables
 exterminateButton= (Button)findViewById (R.id.exterminateButton ) ;      
    }

}

Can you copy in your XML code too? That's where the R.id. is pointing.

Thanks,

Steve.

3 Answers

Hi. The instructions say "the ID for the button is button1 " It looks like you put the wrong id there. Correct it and you should be good to go :)

Still don't get it.

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);

        // Declare our view variables
     exterminateButton1= (Button)findViewById (R.id.exterminateButton1 ) ;

[MOD: edited code block]

You are not calling the view with the id button1 in your code, but a view with another id. Just make sure you use the correct id, as per the instructions. If you're still a bit unsure as to what an id is and how to use it, just re-watch the video and follow the same steps here.

Sorry if I don't serve you the answer on a plate here, but I'm a firm believer in the fact that solving it yourself with a little clue will be more helpful to you in the future. I hope you understand :)

:+1: :smile: