0

just started to learn about building android apps. Initial using Eclipse and get this error by creating my first code:

The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new OnClickListener(){})

Searched a while, tried to fix imports, etc. Here is my code:

package com.example.test1; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.Button; import android.widget.TextView; import android.view.View.OnClickListener; public class MainActivity extends Activity { Button plus, minus; TextView ergebnis; int count; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); plus = (Button) findViewById(R.id.aHead); minus = (Button) findViewById(R.id.bMinus); ergebnis = (TextView) findViewById(R.id.tvOut); count = 0; plus.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { count += 1; ergebnis.setText("Anzahl: " + count); } }); minus.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { count -= 1; ergebnis.setText("Anzahl: " + count); } }); } } 
4
  • post your activity_main.xml Commented Nov 9, 2018 at 9:57
  • No need of import android.view.View.OnClickListener; Commented Nov 9, 2018 at 10:00
  • Why use Eclipse, Use Android Studio, or turn on Automatic imports of classes. Commented Nov 9, 2018 at 10:05
  • The onClick() method is overriden so there should be an @Override indicator. @Override public void onClick(View v) { // Your code here } Commented Nov 9, 2018 at 10:14

1 Answer 1

2

First of all import this

import android.view.View; 

Then set listener for the button override OnClick Method

YourButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //Your Stuff } }); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.