Android Build an Interactive Story App User Input Finding Views by IDs

Nkosinolwazi Moyo
Nkosinolwazi Moyo
9,562 Points

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

}
Steve Hunter
Steve Hunter
Treehouse Moderator 57,546 Points

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

Nkosinolwazi Moyo
Nkosinolwazi Moyo
9,562 Points

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