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 (2014) Basic Android Programming Adding the onClick() Method

Joshua Bivens
Joshua Bivens
8,586 Points

showFactButton.OnClickListener(listener) throws an error that on hover says 'Expected Class or Package.'

What's going on?

package com.sixtytendev.funfacts;

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

import org.w3c.dom.Text;


public class FunFactsActivity extends Activity {

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

        //Declare/assign to variables
        final TextView factLabel = (TextView) findViewById(R.id.factTextView);
        Button showFactButton = (Button) findViewById(R.id.showFactButton);
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String fact = "Ostriches can run faster than horses?";
                factLabel.setText(fact);
            }
        };
        showFactButton.OnClickListener(listener);
    }

It's all imported properly.

3 Answers

william parrish
william parrish
13,774 Points

Sorry, i missed what you actually did wrong.

you need to type factButton.setOnClickListener instead of what you are using ( factButton.onClickListener)

change it to setOnClickListener, and you should be fine.

william parrish
william parrish
13,774 Points

Are you sure that's all of your code?

If so, you are missing a closing brace for the class definition

put another "}" at the end of the file

Joshua Bivens
Joshua Bivens
8,586 Points

No, there's a bit more but it's properly closed.

Joshua Bivens
Joshua Bivens
8,586 Points

The error is 'Error:(31, 23) error: cannot find symbol method OnClickListener(OnClickListener).' Line 31 is when it's called, 23 when it's declared.