Reputation: 95
I'm learning android and I'm currently doing a calculator. I've already done the xml part, where I put the buttons And I'm trying to finish the Java file, which is the code that makes the calculator work
BUT in the Java file I've got this error: Error:(22, 64) error: cannot find symbol class OnClickListener
and I don't know what to do :c I'm still learning I've seen that I should put
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
But I don't understand what's that and I don't know if that will bug the calculator.
That's the Java file (MainActivity)
package com.example.glow.pruebas;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button n0 = (Button) findViewById(R.id.B0);
n0.setOnClickListener(this);
Button n1 = (Button) findViewById(R.id.B1);
n1.setOnClickListener(this);
Button n2 = (Button) findViewById(R.id.B2);
n2.setOnClickListener(this);
Button n3 = (Button) findViewById(R.id.B3);
n3.setOnClickListener(this);
Button n4 = (Button) findViewById(R.id.B4);
n4.setOnClickListener(this);
Button n5 = (Button) findViewById(R.id.B5);
n5.setOnClickListener(this);
Button n6 = (Button) findViewById(R.id.B6);
n6.setOnClickListener(this);
Button n7 = (Button) findViewById(R.id.B7);
n7.setOnClickListener(this);
Button n8 = (Button) findViewById(R.id.B8);
n8.setOnClickListener(this);
Button n9 = (Button) findViewById(R.id.B9);
n9.setOnClickListener(this);
Button coma = (Button) findViewById(R.id.Bcoma);
coma.setOnClickListener(this);
Button igual = (Button) findViewById(R.id.Bigual);
igual.setOnClickListener(this);
Button suma = (Button) findViewById(R.id.B6sumar);
suma.setOnClickListener(this);
Button resta = (Button) findViewById(R.id.B5restar);
resta.setOnClickListener(this);
Button mul = (Button) findViewById(R.id.Bmult);
mul.setOnClickListener(this);
Button division = (Button) findViewById(R.id.Bdividir);
division.setOnClickListener(this);
Button raiz = (Button) findViewById(R.id.raiz);
raiz.setOnClickListener(this);
Button elevado = (Button) findViewById(R.id.BElevado);
elevado.setOnClickListener(this);
Button DEL = (Button) findViewById(R.id.BDEL);
DEL.setOnClickListener(this);
Button AC = (Button) findViewById(R.id.BAC);
AC.setOnClickListener(this);
Button sin = (Button) findViewById(R.id.Bsin);
sin.setOnClickListener(this);
Button cos = (Button) findViewById(R.id.Bcos);
cos.setOnClickListener(this);
Button tan = (Button) findViewById(R.id.Btan);
tan.setOnClickListener(this);
Button secreto = (Button) findViewById(R.id.Bsecreto);
secreto.setOnClickListener(this);
}
@Override
public void onClick(View v) {
TextView pantalla = (TextView) findViewById(R.id.texto);
int seleccion = v.getId();
try {
switch (seleccion) {
case R.id.B0:
pantalla.setText("0");
break;
case R.id.B1:
pantalla.setText("1");
break;
case R.id.B2:
pantalla.setText("2");
break;
case R.id.B3:
pantalla.setText("3");
break;
case R.id.B4:
pantalla.setText("4");
break;
case R.id.B5:
pantalla.setText("5");
break;
case R.id.B6:
pantalla.setText("6");
break;
case R.id.B7:
pantalla.setText("7");
break;
case R.id.B8:
pantalla.setText("8");
break;
case R.id.B9:
pantalla.setText("9");
break;
case R.id.Bcoma:
pantalla.setText(",");
break;
case R.id.Bmult:
break;
case R.id.B5restar:
break;
case R.id.B6sumar:
break;
case R.id.Bdividir:
break;
case R.id.BAC:
break;
case R.id.Bsin:
break;
case R.id.Bcos:
break;
case R.id.Btan:
break;
}
}catch(Exception e){
pantalla.setText("error");
};
}
}
Upvotes: 0
Views: 953
Reputation: 385
As you said you have to add
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
So your problem is here:
public class MainActivity extends ActionBarActivity implements OnClickListener
.
Change that line to
public class MainActivity extends ActionBarActivity implements View.OnClickListener
You are missing the View.OnClickListener and using just implements OnClickListener Changing that will solve your issue ;)
Upvotes: 0
Reputation: 5269
your mistake is that you implemented DialogInterface.onClickListener, it not work as a simple click listener, but in your case you have to implement View.onClickListener
your DialogInterface.onClickListener only work on dialog, and its not a VIEW and you are working on view object thats why.
Upvotes: 0
Reputation: 577
A silly mistake.
Change your implement from OnClickListener
to View.OnClickListener
.
Since you said you don't know what it does:
http://developer.android.com/reference/android/view/View.OnClickListener.html
You are basically trying to implement an Interface, so that you can access its method onClick(View v)
where you are passing the View
in the argument.
Upvotes: 1