I've tried to write the code in Java (I'm using Eclipse) but I get the same error every time and I don't know how to correct it (I'm new to java). When I press the "=" button, or "sin", "cos", etc. (any other button that display a result on the screen) the program just stops running.
What is wrong with the code?
package com.example.proiectcalculator;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Calculator extends Activity {
//public EditText display;
TextView screen;
// int a, result;
int c;
Button b;
int sum1;// sum2, sum;
String y;
int sw=0;
public int getSum1(int sum1) {
return sum1;
}
/* public int getSum2(int sum2) {
return sum2;
}*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
sum1=0;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.calculator, menu);
return true;
}
public void buttonEventHandler(View v) {
screen=(TextView)findViewById(R.id.textView1);
b=(Button)findViewById(R.id.button12);
switch(v.getId()) {
case R.id.button12 :
screen.setText(screen.getText() + "1");
if(sw==0)
{
sw=1;
c = 1;
}
else
c = c*10 + 1;
break;
case R.id.button11 :
screen.setText(screen.getText() + "2");
if(sw==0)
{
c = 2;
sw=1;
}
else
c = c*10 + 2;
break;
case R.id.button10 :
screen.setText(screen.getText() + "3");
if(sw==0)
{
sw=1;
c = 3;
}
else
c = c*10 + 3;
break;
case R.id.button9 :
screen.setText(screen.getText() + "4");
if(sw==0)
{
sw=1;
c = 4;
}
else
c = c*10 + 4;
break;
case R.id.button8 :
screen.setText(screen.getText() + "5");
if(sw==0)
{
sw=1;
c = 5;
}
else
c = c*10 + 5;
break;
case R.id.button7 :
screen.setText(screen.getText() + "6");
if(sw==0)
{
sw=1;
c = 6;
}
else
c = c*10 + 6;
break;
case R.id.button6 :
screen.setText(screen.getText() + "7");
if(sw==0)
{
sw=1;
c = 7;
}
else
c = c*10 + 7;
break;
case R.id.button5 :
screen.setText(screen.getText() + "8");
if(sw==0)
{
sw=1;
c = 8;
}
else
c = c*10 + 8;
break;
case R.id.button4 :
screen.setText(screen.getText() + "9");
if(sw==0)
{
sw=1;
c = 9;
}
else
c = c*10 + 9;
break;
case R.id.button13 :
screen.setText(screen.getText() + "0");
if(sw==0)
{
sw = 1;
c = 0;
}
else
c = c*10;
break;
/* case R.id.button1 :
if( c!=0 )
screen.setText(screen.getText() - c);
break;*/
case R.id.button2 :
screen.setText("");
c = 0;
break;
case R.id.button16 :
screen.setText(screen.getText() + "+");
y="+";
//salvam numarul de dinainte de un semn
sum1=c;
sw=0;
c=0;
break;
case R.id.button17 :
screen.setText(screen.getText() + "-");
y="-";
sum1=c;
sw=0;
c=0;
break;
case R.id.button18 :
screen.setText(screen.getText() + "*");
y="*";
sum1=c;
sw=0;
c=0;
break;
case R.id.button19 :
screen.setText(screen.getText() + "/");
y="/";
sum1=c;
sw=0;
c=0;
break;
case R.id.button15 :
if( y=="+" )
screen.setText(sum1 + c);
else
if( y=="*" )
screen.setText(sum1 * c);
else
if( y=="/" )
screen.setText(sum1 / c);
else
if( y=="-" )
screen.setText(sum1 - c);
else
if( y=="^" )
screen.setText((int)Math.pow((double)sum1, (double)c));
c = 0;
sum1=0;
sw=0;
break;
case R.id.button20 :
sum1 = c;
screen.setText((int) Math.sin(sum1));
sw=0;
c=0;
sum1=0;
break;
case R.id.button21 :
sum1=c;
screen.setText((int) Math.cos(sum1));
sw=0;
c=0;
sum1=0;
break;
case R.id.button22 :
sum1=c;
screen.setText((int) Math.tan(sum1));
sw=0;
c=0;
sum1=0;
break;
case R.id.button23 :
sum1=c;
screen.setText((int) (1/(Math.tan(sum1))));
sw=0;
c=0;
sum1=0;
break;
case R.id.button27 :
sum1=c;
screen.setText(sum1*sum1);
sw=0;
c=0;
sum1=0;
break;
case R.id.button28 :
sum1=c;
screen.setText(sum1*sum1*sum1);
sw=0;
c=0;
sum1=0;
break;
case R.id.button32 :
sum1=c;
screen.setText((int)(1/sum1));
sw=0;
c=0;
sum1=0;
break;
case R.id.button33 :
sum1=c;
sw=0;
y="/";
screen.setText(screen.getText() + "/");
c=0;
break;
case R.id.button29 :
sum1=c;
sw=0;
y="^";
screen.setText(screen.getText() + "^");
c=0;
break;
case R.id.button31 :
sum1=c;
sw=0;
screen.setText((int)(Math.sqrt((double)sum1)));
sum1=0;
c=0;
break;
case R.id.button25 :
sum1=c;
sw=0;
screen.setText((int)Math.exp((double)(sum1)));
sum1=0;
c=0;
break;
case R.id.button36 :
sum1=c;
sw=0;
screen.setText((int)((sum1*(sum1+1))/2));
sum1=0;
c=0;
break;
case R.id.button38 :
sum1=c;
sw=0;
screen.setText((int)(Math.toRadians((double)(sum1))));
sum1=0;
c=0;
break;
case R.id.button26 :
sum1=c;
sw=0;
screen.setText((int)(Math.toDegrees((double)(sum1))));
sum1=0;
c=0;
break;
case R.id.button24 :
sum1=c;
sw=0;
screen.setText((int)(Math.log((double)(sum1))));
sum1=0;
c=0;
break;
}
}
}
And here is the .xml file:
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/button2"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button4"
android:layout_below="@+id/button4"
android:onClick="buttonEventHandler"
android:text="6" />
<Button
android:id="@+id/button8"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button5"
android:layout_alignTop="@+id/button7"
android:onClick="buttonEventHandler"
android:text="5" />
<Button
android:id="@+id/button9"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button6"
android:layout_alignTop="@+id/button8"
android:onClick="buttonEventHandler"
android:text="4" />
<Button
android:id="@+id/button10"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button7"
android:layout_below="@+id/button7"
android:onClick="buttonEventHandler"
android:text="3" />
<Button
android:id="@+id/button11"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button8"
android:layout_alignTop="@+id/button10"
android:onClick="buttonEventHandler"
android:text="2" />
<Button
android:id="@+id/button12"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button9"
android:layout_below="@+id/button9"
android:onClick="buttonEventHandler"
android:text="1" />
<Button
android:id="@+id/button13"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button10"
android:layout_below="@+id/button10"
android:onClick="buttonEventHandler"
android:text="0" />
<Button
android:id="@+id/button17"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button16"
android:layout_alignTop="@+id/button9"
android:onClick="buttonEventHandler"
android:text="-" />
<Button
android:id="@+id/button18"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button17"
android:layout_alignTop="@+id/button12"
android:onClick="buttonEventHandler"
android:text="*" />
<Button
android:id="@+id/button19"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button18"
android:layout_alignTop="@+id/button15"
android:onClick="buttonEventHandler"
android:text="/" />
<Button
android:id="@+id/button21"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button20"
android:layout_below="@+id/button20"
android:onClick="buttonEventHandler"
android:text="cos" />
<Button
android:id="@+id/button22"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button18"
android:layout_alignBottom="@+id/button18"
android:layout_alignLeft="@+id/button21"
android:onClick="buttonEventHandler"
android:text="tan" />
<Button
android:id="@+id/button23"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button22"
android:layout_below="@+id/button22"
android:onClick="buttonEventHandler"
android:text="cotan" />
<Button
android:id="@+id/button27"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button13"
android:layout_below="@+id/button13"
android:text="x^2"
android:onClick="buttonEventHandler" />
<Button
android:id="@+id/button28"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button27"
android:layout_below="@+id/button27"
android:text="x^3"
android:onClick="buttonEventHandler" />
<Button
android:id="@+id/button26"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button36"
android:layout_alignLeft="@+id/button23"
android:onClick="buttonEventHandler"
android:text="Deg" />
<Button
android:id="@+id/button31"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button28"
android:layout_alignLeft="@+id/button15"
android:text="√"
android:onClick="buttonEventHandler" />
<Button
android:id="@+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:layout_marginLeft="20dp"
android:text="9"
android:onClick="buttonEventHandler" />
<Button
android:id="@+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button7"
android:layout_toRightOf="@+id/button7"
android:text="8"
android:onClick="buttonEventHandler" />
<Button
android:id="@+id/button15"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button11"
android:layout_below="@+id/button11"
android:onClick="buttonEventHandler"
android:text="=" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_toRightOf="@+id/button31"
android:text="CE"
android:onClick="buttonEventHandler" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView1"
android:layout_marginTop="28dp"
android:layout_toRightOf="@+id/button1"
android:onClick="buttonEventHandler"
android:text="C" />
<Button
android:id="@+id/button32"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button31"
android:layout_alignLeft="@+id/button12"
android:text="1/x"
android:onClick="buttonEventHandler" />
<Button
android:id="@+id/button24"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button31"
android:layout_toRightOf="@+id/button31"
android:text="log"
android:onClick="buttonEventHandler" />
<Button
android:id="@+id/button29"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button28"
android:layout_alignBottom="@+id/button28"
android:layout_toRightOf="@+id/button28"
android:text="x^y"
android:onClick="buttonEventHandler" />
<Button
android:id="@+id/button38"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button24"
android:layout_alignBottom="@+id/button24"
android:layout_alignLeft="@+id/button26"
android:text="Rad"
android:onClick="buttonEventHandler" />
<Button
android:id="@+id/button20"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button9"
android:layout_toRightOf="@+id/button2"
android:onClick="buttonEventHandler"
android:text="sin" />
<Button
android:id="@+id/button16"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button21"
android:layout_toLeftOf="@+id/button21"
android:onClick="buttonEventHandler"
android:text="+" />
<Button
android:id="@+id/button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button8"
android:layout_toRightOf="@+id/button8"
android:onClick="buttonEventHandler"
android:text="7" />
<Button
android:id="@+id/button25"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button24"
android:layout_toRightOf="@+id/button24"
android:onClick="buttonEventHandler"
android:text="Exp" />
<Button
android:id="@+id/button33"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button24"
android:layout_toRightOf="@+id/button31"
android:text="x/y"
android:onClick="buttonEventHandler" />
<Button
android:id="@+id/button36"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button25"
android:layout_toRightOf="@+id/button24"
android:text="Sum"
android:onClick="buttonEventHandler" />