Android Build a Simple Android App with Java Basic Android Programming Initializing a Button

Farai Ted Mandoreba
Farai Ted Mandoreba
18,583 Points

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

Farai Ted Mandoreba
Farai Ted Mandoreba
18,583 Points

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