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 a Simple Android App (retired 2014) Getting Started with Android Accessing Views in Code

Make sure to use "findViewById" to declare the button.

I really don't know how to solve this problem. I've been stuck for 3 days in a row, please help me!!

This is the code:

'''Java

package com.example.crystal.ball;

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

public class MainActivity extends Activity {

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

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

}

'''

6 Answers

Jason Thorn
Jason Thorn
2,611 Points

Hi Yonatan

It looks like you are trying to assign a button (R.id.button1) to a TextView object. you need to put ->

TextView answerLabel = (TextView) findViewById(R.id.textView1);
Button getAnswerButton = (Button) findViewById(R.id.button1);

Thats providing your textView and button are named textView1 & button1 - you can check them in your activity_main.xml file or use content assist :)

Hello Jason.

Thanks for helping me, I really appreciate it my friend but the error keeps going. I don't know what I'm missing. And thanks again for you help.

'''Java package com.example.crystal.ball;

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

public class MainActivity extends Activity {

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

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

}

'''

Jason Thorn
Jason Thorn
2,611 Points

No worries, not 100% sure as to why that didn't work. What is the error your getting? Also could you post the code in your activity_main.xml

The same, it says Make sure to use "findViewById" to declare the button. Where is my activity_main.xml?

Jason Thorn
Jason Thorn
2,611 Points

it's under res/layout/

Should be something like activity_main_list.xml. Also is the line -

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

underlined in red?

No, it not underlined in red my friend