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 Making a Button Do Something

Juan Gonzalez
Juan Gonzalez
11,848 Points

setOnClickListener and listener highlighted in red

As the title states i have almost an exact copy of the code used in the video and setOnClickListener is a unresolved symbol. Also, in the parentheses after it listener is red as well. Please Help,

P.S Code is exactly the same as the end of the video. I made sure i had the view import and not the dialog and now i am stuck. :c

3 Answers

Hi Juan,

It appears you've left out the View for your new OnClickListener, then on the last line you're trying to set a generic OnClickListener instead of the one you've defined as the variable listener above. You've also closed your onCreate function after setContentView, without first including that you've given the button a listener. You want the button to be 'listening' for a click as soon as the main Activity which includes the button is created. Lastly, it looks like you're missing a curley bracket at the end.

So to correct your code, I think you'd want something like;

package com.mycompany.funfacts;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class FunFactsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //  Assign the layout to this activity
        setContentView(R.layout.activity_fun_facts);

        // Declare our View variables and assign them the Views from the layout file
       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 v) {

            }
       };
    showFactButton.setOnClickListener(listener);
    }
}
Juan Gonzalez
Juan Gonzalez
11,848 Points

Thank you so much for the help! I appreciate your time.

Juan,

Hi Juan,

you could download the Project Files - which are listed under the video in the Downloads section - and compare to your code to see what is different, or you could try posting the specific code here to see if there might be a typo or missing semicolon somewhere.

Good luck!

Hi Juan,

If you have an unresolved symbol, there's something that's not defined.

Can you post your code, please? We can see what the issue is, then.

Thanks,

Steve.

Juan Gonzalez
Juan Gonzalez
11,848 Points

Thanks for your time! First of all i cannot even see the quick documentation of setOnClickListener. It's completely red as if it does not exist. Listener is only underlined in red. I tried downloading the same sdk and api the video shows. This is the only problem i have encountered so far that i cannot troubleshoot. Would love to keep learning as soon as possible! Thank you for your help! here is my code..

Code:

package com.mycompany.funfacts;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class FunFactsActivity extends AppCompatActivity {

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

    // Declare our Veiw variables and assign them the Veiws from the layout file
    TextView factLabel = (TextView) findViewById(R.id.factTextView);
    Button showFactButton = (Button) findViewById(R.id.showFactButton);
    View.OnClickListener listener = new OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    };
    showFactButton.setOnClickListener(View.OnClickListener);