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 a Simple Android App with Java Basic Android Programming Initializing a Button

Initialize the showFactButton variable using the findViewById() method like the TextView above it. The ID for the button

what is the correct answer mine is wrong, and is pointing at shawfactbutton pointing at the "s"

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



        Button showFactButton = (Button) findViewById(R.id.showFactButton);
        TextView factLabel = (TextView) findViewById(R.id.factTextView);
        Button showFactButton;


    }
}

1 Answer

Hi Farai. The solution for this challenge was posted in your other post. But let's have a look at the problem here, just so you know. When clicking on Preview, you see:

./FunFactsActivity.java:18: error: variable showFactButton is already defined in method onCreate(Bundle)
        Button showFactButton;
               ^
1 error

It says "error: variable showFactButton is already defined in method onCreate(Bundle)" If you look at the code, you will see that Button showFactButton is indeed defined twice in the code:

Button showFactButton = (Button) findViewById(R.id.showFactButton);  // Defined here
TextView factLabel = (TextView) findViewById(R.id.factTextView);
Button showFactButton;  // Defined here

Just remove the last line (Button showFactButton;) and you'll be fine. Hope that helps :)

thanks a lot it helped, others will actually add more confusion