Veeresh
Veeresh

Reputation: 373

Cannot find symbol class onClickListener

I'm new to android development. Here's the problem i ran into. I'm using Android Studio. I looked up on many sites, they said to import the related class. Having done that, the problem remains. Any help is appreciated.


Can anyone please help me with this, i have been searching for a while now for the solution.

Here's the code:

package com.example.veeresh.myapplication;
//import statements
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

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

        Button button1 = (Button)findViewById(R.id.button1);

        button1.setOnClickListener(
                //error: cannot find symbol class onClickListener
                new Button.onClickListener()
                {
                    public void onClick(View v)
                    {
                        TextView text1 = (TextView)findViewById(R.id.text1);
                        text1.setText("Veeresh Here");
                    }
                }
                );
    }
}

Error:

Error:(24, 27) error: cannot find symbol class onClickListener
Error:Execution failed for task ':app:compileDebugJava'.
Compilation failed; see the compiler error output for details.

Upvotes: 6

Views: 17638

Answers (4)

Erfan GLMPR
Erfan GLMPR

Reputation: 968

Both Button.OnClickListener() and View.OnClickListener() works fine it is just the capital O that you are missing.

Upvotes: 0

Bala Saikrupa Puram
Bala Saikrupa Puram

Reputation: 721

update your code with the below code

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

public class MainActivity extends Activity implements android.view.View.OnClickListener {

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

        Button button1 = (Button)findViewById(R.id.button1);

        button1.setOnClickListener(this);
    }


    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        TextView text1 = (TextView)findViewById(R.id.text1);
        text1.setText("Veeresh Here");

    }
}

Upvotes: 0

Sachin
Sachin

Reputation: 558

Edit your code like this

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

Button button1 = (Button)findViewById(R.id.button1);
TextView text1 = (TextView)findViewById(R.id.text1);

button1.setOnClickListener(new View.onClickListener()
        {
            public void onClick(View v)
            {

                text1.setText("Veeresh Here");
            }
        }
);

Upvotes: 0

sonic
sonic

Reputation: 1904

It should be new View.OnClickListener() instead of new Button.onClickListener()

OnClickListener with a capital O.

Upvotes: 7

Related Questions