A_Thorne
A_Thorne

Reputation: 141

OnClickListener issue in android

package com.example.firstproject;


import android.app.Activity;



import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.view.View;
public class MainActivity extends Activity {

int counter;

TextView display;
Button add,sub;

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

       counter=0;
       add=(Button)findViewById(R.id.bAdd);
       sub=(Button)findViewById(R.id.bSub);
       display =(TextView)findViewById(R.id.tvDisplay);

      add.setOnClickListener(new OnClickListener......);

     }
}

problem is: OnClickListener cannot be resolved to a variable

i have checked other posts and fix it for imports but still not getting the reason

Upvotes: 0

Views: 79

Answers (2)

Yauraw Gadav
Yauraw Gadav

Reputation: 1746

add.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
        //IMPLEMENTATION
    }
});

Upvotes: 0

Leandros
Leandros

Reputation: 16825

Try it like this:

add.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        // Your code here.
    }
});

Upvotes: 2

Related Questions