Answer the question
In order to leave comments, you need to log in
Calculator on Android crashes when displaying the result. What is the reason?
I just started to learn Android, and as a training I decided to rewrite this simple calculator - cybern.ru/calcjava.html on Android as a graphical application. In Android Studio I made an elementary graphical interface and rewrote the application as follows:
public class MainActivity extends AppCompatActivity {
public double FirstValue;
String operator;
TextView MyView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyView = (TextView)findViewById(R.id.MyView);
}
public void ClickOne(View view){
MyView.setText(MyView.getText() + "1");
}
public void ClickTwo(View view){
MyView.setText(MyView.getText() + "2");
}
public void ClickThree(View view){
MyView.setText(MyView.getText() + "3");
}
public void ClickFour(View view){
MyView.setText(MyView.getText() + "4");
}
public void ClickFive(View view){
MyView.setText(MyView.getText() + "5");
}
public void ClickSix(View view){
MyView.setText(MyView.getText() + "6");
}
public void ClickSeven(View view){
MyView.setText(MyView.getText() + "7");
}
public void ClickEight(View view){
MyView.setText(MyView.getText() + "8");
}
public void ClickNine(View view){
MyView.setText(MyView.getText() + "9");
}
public void ClickNull(View view){
MyView.setText(MyView.getText() + "0");
}
public void ClickN(View view){
CharSequence FirstValur_str = MyView.getText();
String FirstValurString = String.valueOf(FirstValur_str);
FirstValue = Double.valueOf(FirstValurString);
MyView.setText("");
operator = "/";
}
public void ClickM(View view){
CharSequence FirstValur_str = MyView.getText();
String FirstValurString = String.valueOf(FirstValur_str);
FirstValue = Double.valueOf(FirstValurString);
MyView.setText("");
operator = "*";
}
public void ClickPlus(View view){
CharSequence FirstValur_str = MyView.getText();
String FirstValurString = String.valueOf(FirstValur_str);
FirstValue = Double.valueOf(FirstValurString);
MyView.setText("");
operator = "+";
}
public void ClickMinus(View view){
CharSequence FirstValur_str = MyView.getText();
String FirstValurString = String.valueOf(FirstValur_str);
FirstValue = Double.valueOf(FirstValurString);
MyView.setText("");
operator = "-";
}
public void ClickClear(View view){
FirstValue = 0;
MyView.setText("");
}
public void ClickRes(View view){
CharSequence FirstVaur_str = MyView.getText();
String FirstValueString = String.valueOf(FirstVaur_str);
double NextValue = Double.valueOf(FirstValueString);
if ("+".equals(operator)){
String ResultGetTextString = String.valueOf(FirstVaur_str);
double Result = Double.valueOf(ResultGetTextString) + NextValue;
MyView.setText((int) Result);
}
if("-".equals(operator)){
String ResultGetTextString = String.valueOf(FirstVaur_str);
double Result = Double.valueOf(ResultGetTextString) - NextValue;
MyView.setText((int) Result);
}
if ("*".equals(operator)){
String ResultGetTextString = String.valueOf(FirstVaur_str);
double Result = Double.valueOf(ResultGetTextString) * NextValue;
MyView.setText((int) Result);
}
if ("/".equals(operator)){
String ResultGetTextString = String.valueOf(FirstVaur_str);
double Result = Double.valueOf(ResultGetTextString) / NextValue;
MyView.setText((int) Result);
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question