I’m programming a Android Calculator. I have all my code, but when use the calculator, it always prints “0.0”, which is the original I set for total1 and total2. So, my assumption is that the methods are not seeing those two variables. So I assume I have to set them as static variables so that the rest of my code can use them and change them. I may be setting up the static variable wrong. But after setting total1 and total2 as static variables, I still have the same problem. My other guess about why my program isn’t working is my displayValue variable. In my if statement, it says that the local variable displayValue is not used. I would appreciate any help.
package rechee.cool;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class HelloAndroidActivity extends Activity {
/** Called when the activity is first created. */
public EditText display;
String display1;
double displayValue;
// I want to be able to use the following two variables everywhere in the code.
static double total1=0.0;
static double total2=0.0;
char theOperator;
public String buttonText;
public Button ButtonAdd, ButtonEqual, ButtonMultiply, ButtonDivide, ButtonMinus;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
display= (EditText) findViewById(R.id.editText1);
if(display.length()!=0){
String display1= display.getText().toString();
// Says local variable displayValue has not been used. May be my problem?
double displayValue= Double.parseDouble(display1);
}
}
public void getOperator(String btnText){
theOperator = btnText.charAt(0);
total1+=displayValue;
display.setText("");
}
public void onClick(View v) {
switch(v.getId()){
case R.id.bOne:
display.append("1");
break;
case R.id.bTwo:
display.append("2");
break;
case R.id.bThree:
display.append("3");
break;
case R.id.bFour:
display.append("4");
break;
case R.id.bFive:
display.append("5");
break;
case R.id.bSix:
display.append("6");
break;
case R.id.bSeven:
display.append("7");
break;
case R.id.bEight:
display.append("8");
break;
case R.id.bNine:
display.append("9");
break;
case R.id.bZero:
display.append("0");
break;
case R.id.bPoint:
display.append(".");
break;
case R.id.bClear:
total2= 0.0;
display.setText("");
break;
case R.id.bAdd:
buttonText="+";
ButtonAdd= (Button)findViewById(R.id.bAdd);
ButtonAdd.setText(buttonText);
getOperator(buttonText);
break;
case R.id.bMinus:
buttonText="-";
ButtonMinus= (Button)findViewById(R.id.bMinus);
ButtonMinus.setText(buttonText);
getOperator(buttonText);
break;
case R.id.bMultiply:
buttonText="*";
ButtonMultiply= (Button)findViewById(R.id.bMultiply);
ButtonMultiply.setText(buttonText);
getOperator(buttonText);
break;
case R.id.bDivide:
buttonText="/";
ButtonDivide= (Button)findViewById(R.id.bDivide);
ButtonDivide.setText(buttonText);
getOperator(buttonText);
break;
case R.id.bEqual:
switch (theOperator){
case '+':
total2= total1 + displayValue;
break;
case '-':
total2= total1 - displayValue;
break;
case '*':
total2= total1 * displayValue;
break;
case '/':
total2= total1 / displayValue;
break;
}
display.setText(Double.toString(total2));
total1=0.0;
break;
}
}
}
You’re re-declaring variables local only to the scope of
onCreate. TrySuggested reading