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 Basic Android Programming Initializing a Button

The challenge has been kicking my head for the past four days and I really need help. Initialize the ShowFactButton var

Initialize the ShowFactButton variable using the findViewById() method like the TextView above it. The ID for the button is ShowFactButton, and don't forget to cast it to a button with (Button)!

My code: mShowFactButton=(Button) findButtonById(R.id.showFactButton); I got error reading and I am confused.

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

public class FunFactsActivity extends Activity {

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

        // Declare our View variables and assign them the Views from the layout file
        TextView factLabel = (TextView) findViewById(R.id.factTextView);
        Button showFactButton;
    }mshowFactButton=(Button) findButtonById(R.id.showFactButton);
}

2 Answers

mShowFactButton=(Button) findButtonViewBYId(R.id.showFactButton);

I keep receiving error from this code. What is the problem? Please help!

Brian Pedigo
Brian Pedigo
26,783 Points

Hi Daniel, after looking over your code it looks like you have Initialize the ShowFactButton var out side of the the onCreate method. Also, you should be naming the variable showFactButton and not mShowFactButton the m prefix wasn't needed on this one. Try the following:

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

public class FunFactsActivity extends Activity {

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

        // Declare our View variables and assign them the Views from the layout file
        TextView factLabel = (TextView) findViewById(R.id.factTextView);
        //Try initializing the button like this! 
        Button showFactButton = (Button) findViewById(R.id.showFactButton);
    }
}

I hope this helps. Happy codding!

Hi Brian,

I used this code and it didn't work.

Button showFactButton = (Button) findButtonById(R.id.showFactButton);

Brian Pedigo
Brian Pedigo
26,783 Points

Hi Daniel when I looked at this code the first time I only noticed that the variable was being created outside of the on create method and didn't also notice that the findButtonByid method call was being used. To fix this change the findButtonById call to findViewById. Should look like this:

Button showFactButton = (Button) findViewById(R.id.showFactButton);