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

hello,how can I solve this problem ?No matter how hard I try ,it can not through ,give me a hand please,thanks.

hello,how can I solve this problem ?No matter how hard I try ,it can not through ,I don't know how to solve this ,give me a hand please,thanks.

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) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Declare our view variables

    }

}

2 Answers

Let's do this!

Initialize the member variable mExterminateButton using the findViewById() method. The ID for the button is button1. Don't forget to cast the generic View returned by the method!

We have mExterminateButton declared in the class, we to initialize inside onCreate(). The GUI has a button called button1 that needs linking to this variable in the code. We use findViewById() to do this, casting the returned object to the required type. That's a Button in this case.

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

That should do it!

Steve.

yes,I also write like yours,but it show me it is wrong!What happened?

Let me try that again ... ?!

That code does work:

http://goo.gl/QgG6yh

Can you paste your code, please?

Steve.

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) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

}

Thanks,the code I have paste there.

That's really strange.

I suggest you hit Ctrl+F5 to reload the challenge fully and try that code again - there seems nothing wrong with it!

ok,Thank you,if I have problem,I will ask you again.Thank you very much .

Please do, yes - let's got you through this!

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) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

}