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

zarah hafeez
zarah hafeez
317 Points

Getting error in my code, even I'm following this tutorial.

package com.example.i7.myfunfactproject;

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

public class FunFactActivity extends AppCompatActivity {
private TextView mFunFactView;
    private Button mShowFactButton;


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

        mFunFactView = (TextView) findViewById(R.id.FunFactView);
        mShowFactButton = (Button) findViewById(R.id.ShowFactButton);

        View.OnClickListener Listner = new View.OnClickListener(){

            @Override
            public void onClick(View v) {

            }
        };

        mShowFactButton.OnClickListener(Listner);
    }
}

Messages: Information:Gradle tasks [:app:incrementalDebugSupportDex] :app:buildInfoDebugLoader :app:incrementalDebugJavaCompilationSafeguard UP-TO-DATE :app:compileDebugJavaWithJavac C:\Users\i7\AndroidStudioProjects\MyFunFactProject\app\src\main\java\com\example\i7\myfunfactproject\FunFactActivity.java Error:(30, 24) error: cannot find symbol method OnClickListener() Incremental compilation of 1 classes completed in 1.633 secs. :app:compileDebugJavaWithJavac FAILED Error:Execution failed for task ':app:compileDebugJavaWithJavac'.

Compilation failed; see the compiler error output for details. Information:BUILD FAILED Information:Total time: 7.176 secs Information:2 errors Information:0 warnings Information:See complete output in console

2 Answers

Hi Zarah,

I think this line:

mShowFactButton.OnClickListener(Listener);

... needs to be ...

mShowFactButton.setOnClickListener(Listener);

Let me know how you get on.

Steve.

aakarshrestha
aakarshrestha
6,509 Points

You need to set the button to do the click action. For that you can do it multiple ways:

mShowFactButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //do something here
            }
        });

or

 View.OnClickListener Listner = new View.OnClickListener(){

            @Override
            public void onClick(View v) {

            }
        };

        mShowFactButton.setOnClickListener(Listner);

Hope you understand how it can be done in multiple ways.

Happy coding!