R
R
retr02017-03-23 16:08:45
Java
retr0, 2017-03-23 16:08:45

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);
         }
     }
}

The DEL,% and brackets buttons have not yet been implemented. When you click on "=" after the add/subtract/divide/multiply operation, the application crashes. What's the problem?7353aa6c7444479eaa985b078b4a1a1f.jpg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AlexeyVD, 2017-03-23
@retr0

You could at least copy the stack trace here, or you could look at it yourself and solve the problem.
View.setText() accepts either CharSequence or resource id, and you are trying to put an int result there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question