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

Hi, I can't add .setOnClickListener(listener) to my button.. Please help! thanks!

I'm working on an alteration to teamtreehouse's "build a simple android app"

everything works except the last line where I cant setup the listener to the button.

package com.jessicanahulan.funtime;

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

public class FunFacts extends Activity {

private FactBook mFactBook = new FactBook();
private ColorWheel mColorWheel = new ColorWheel();

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


final CheckBox simonButton = (CheckBox) findViewById(R.id.simonOption);
final CheckBox kelButton = (CheckBox) findViewById(R.id.kelOption);
final Button contButton = (Button) findViewById(R.id.continueButton);


View.OnClickListener Listener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.continueButton) {

            if (kelButton.isChecked() && !simonButton.isChecked()) {
                //setContentView(R.layout.activity_fun_facts);
            } else if (simonButton.isChecked() && !kelButton.isChecked()) {
                // setContentView(R.layout.activity_fun_facts);
            }

        }
    }
};

   contButton.setOnClickListener(listener);  //error      

//.setOnClickListener is underlined in red

}

Your parameter "listener" should be written with a capital L since the OnClickListener "Listener" is written with a capital L when declared.

1 Answer

That line should be written like this:

 contButton.setOnClickListener(Listener);

Hi!! Thanks so much for your response. I fixed that line as you said and I think that would have definitely been a problem with thhe code! But I still get the .setOnClickListener as underlined in red still.. I'm really unsure why. When I type "contButton." I get suggestions like "OnClickListener (android.view.View)", "AccessibilityDelegate (android.view.View)" . ".setOnClickListener" isnt really an option.. i dont know if there is a problem with my button type? I've definitely imported the import android.widget.Button; and I think I've followed the steps correctly... Thanks again for your help!

I believe I have found the problem with your code, your onCreate closing curly brace is placed right after the setContentView function, it should not be place there, it belongs right after the line that is being underlined in red. The problem was that this line was out of scope for the onCreate method. The following code should work.

public class FunFacts extends Activity {

private FactBook mFactBook = new FactBook();
private ColorWheel mColorWheel = new ColorWheel();

@Override
protected void onCreate(Bundle savedInstanceState) {     // opening brace
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

// } <-- this did not belong here

final CheckBox simonButton = (CheckBox) findViewById(R.id.simonOption);
final CheckBox kelButton = (CheckBox) findViewById(R.id.kelOption);
final Button contButton = (Button) findViewById(R.id.continueButton);


View.OnClickListener Listener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.continueButton) {

            if (kelButton.isChecked() && !simonButton.isChecked()) {
                //setContentView(R.layout.activity_fun_facts);
            } else if (simonButton.isChecked() && !kelButton.isChecked()) {
                // setContentView(R.layout.activity_fun_facts);
            }

        }
    }
};

   contButton.setOnClickListener(Listener);  // this line was out of scope
    } //closing brace  this is where it belongs to ensure that the above line is in scope
}

Thank you sooooooo much!! That fixed it! I've been sooo stumped by this for days!! I'm really new to this! hehe.. thanks!!