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 Adding the OnClick Method

Kendall Chung
Kendall Chung
9,511 Points

cant run my app

hey guys, for some reason I cannot run my app. here are the details of the errors. there are 3 errors preventing me from running the app.

Information:Gradle tasks [:app:assembleDebug] Error:(32, 24) error: method setOnClickListener in class View cannot be applied to given types; required: OnClickListener found: no arguments reason: actual and formal argument lists differ in length Error:(22, 53) error: cannot find symbol variable ShowFactButton Error:Execution failed for task ':app:compileDebugJavaWithJavac'.

Compilation failed; see the compiler error output for details.

Thanks guys!

Kendall Chung
Kendall Chung
9,511 Points

package kendall.funfacts;

import android.content.DialogInterface; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView;

public class FunFactsActivity extends AppCompatActivity { // Declare Our Views variables private TextView mFactTextView; private Button mShowFactButton;

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

    //Assign the Views from the layout file to the corresponding variables
    mFactTextView = (TextView) findViewById(R.id.factTextView);
    mShowFactButton = (Button) findViewById(R.id.ShowFactButton);

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String[] facts= {
                    "Ants stretch when they wake up in the morning.",
                    "Ostriches can run faster than horses.",
                    "Olympic gold medals are actually made mostly of silver.",
                    "You are born with 300 bones; by the time you are an adult you will have 206.",
                    "It takes about 8 minutes for light from the Sun to reach Earth.",
                    "Some bamboo plants can grow almost a meter in just one day.",
                    "The state of Florida is bigger than England.",
                    "Some penguins can leap 2-3 meters out of the water.",
                    "On average, it takes 66 days to form a new habit.",
                    "Mammoths still walked the earth when the Great Pyramid was being built." };

            // The button was clicked, so update the fact TextView with a new fact
            String fact = "";
            // Randomly select a fact
            Random randomGenerator = new Random();
            int randomNumber = randomGenerator.nextInt(10);
            fact = facts[randomNumber];

            // Update the screen with our dymanic fact
            mFactTextView.setText(fact);
        }
    };
    mShowFactButton.setOnClickListener();

}

}

1 Answer

Kourosh Raeen
Kourosh Raeen
23,733 Points

The id of the mShowFactButton button should be showFactButton, with a lower-case s:

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

Also, you need to pass the OnClickListener listener to the setOnClickListener() method:

mShowFactButton.setOnClickListener(listener);