-2

I've been trying to get a simple calculator app working for the past couple of days but I can't seem to narrow the problem. I am a total newbie at android development so there could be a problem with my mainActivity code but I've made some precautions and tried them out. I tried deleting all the code I personally made and redoing the whole emulator process and the app still does not open. I have tried deleting the app on the emulator and restarting, I tried rebuilding, cleaning and turning off instant run. I don't believe it's an Android Studio issue because I created a new project and tossed some XMl elements in and it ran perfectly fine on my computer. I think there's something wrong with my code.

I have deleted code I've written and running the application to no avail. I have uninstalled the application on the emulator and ran it again. I have trying rebuilding and cleaning the project. I have tried syncing the gradle. And I have tried turning on instant run. By the title this may seem like the same thing but it isn't.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    buttonAdd = findViewById(R.id.Add_Button);
    buttonSub = findViewById(R.id.Sub_Button);
    buttonDiv = findViewById(R.id.Div_Button);
    buttonMult = findViewById(R.id.Mul_Button);
    mText = findViewById(R.id.Answer);

    //Conversion of textview to double
    UserInput = findViewById(R.id.FirstNumber);
    UserInput2 = findViewById(R.id.SecondNumber);

    userinput = Double.parseDouble(UserInput.getText().toString());
    userinput2 = Double.parseDouble(UserInput2.getText().toString());

    //functions for the operations.
    buttonAdd.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View a){
     mText.setText("Answer = " + Double.toString((userinput+userinput2)));
        }
    });

    buttonSub.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
  mText.setText("Answer = " + Double.toString((userinput - userinput2)));
        }
    });

    buttonMult.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
     mText.setText("Answer = " + Double.toString((userinput*userinput2)));
        }
    });

    buttonDiv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
  mText.setText("Answer = " + Double.toString ((userinput / userinput2)));
        }
    });


}

}

I would expect to run the program but it doesn't run. I'm hoping that the above code is enough because I must be making an obvious mistake.

LogCat:

My LogCat Errors

4 Answers4

1

I've figured it out! It was a number exception error. My .getText() values are trying to get values before the user even clicks anything. In my case, these editText values were set to "Input first number" and "Input second number" respectively. You can't hope to get an integer value from a string! So obviously it threw it. After putting those two .getText() things inside each onClick, it was fixed. I appreciate the help guys! I've learned a lot about the whole debugging process in the last 15 minutes, I appreciate it. Thanks again!

0

You use 2 TextViews: UserInput and UserInput2 which you use to get the user's input!!
How? TextViews are not editable. You should have used EditTexts.
So when your app starts you get their text with these lines:

userinput = Double.parseDouble(UserInput.toString());
userinput2 = Double.parseDouble(UserInput2.toString());

Do these TextViews contain any text and if yes is it valid number?
If not then an error will be thrown because you try to cast text to number but that text cannot be casted.
But even if the TextViews contained valid numbers the way you use to obtain their text is wrong, it should be by getText().toString():

userinput = Double.parseDouble(UserInput.getText().toString());
userinput2 = Double.parseDouble(UserInput2.getText().toString());

Edit
These 2 lines above copy them in all 4 listeners just before the calculation.
After that delete them from where you originally had placed them because when you start the app the EditTexts are empty and if you try to cast them to a number the app crashes.

forpas
  • 160,666
  • 10
  • 38
  • 76
  • Thanks for the reply! Unfortunately, it's still not working. I changed it to EditTexts and used your code above but it still isn't running. – Earth Patel Mar 31 '19 at 21:01
  • It still isn't running does not mean anything. You must learn to debug. When an app crashes or does not even start there is an error log that informs you about the error that caused the crash. My answer addressed only the obvious problems with your code. – forpas Mar 31 '19 at 21:05
  • I've given a link for the logCat. I looked up the error code for it and I didn't get a satisfactory solution. – Earth Patel Mar 31 '19 at 21:07
0

Your problem lies here:

UserInput = findViewById(R.id.FirstNumber);
userinput = Double.parseDouble(UserInput.toString());

Your UserInput variable is an instance of TextView. You are trying to extract the current text of the TextView, but toString() is not the right method to do that, it only prints out metadata about that class. You can replace it with:

Double.parseDouble(UserInput.getText().toString());

urgentx
  • 3,832
  • 2
  • 19
  • 30
0

Your made a problem by writing

 userinput = Double.parseDouble(UserInput.getText().toString());
    userinput2 = Double.parseDouble(UserInput2.getText().toString());

in onCreate() function. onCreate() only runs when the application starts at first. and is currently the only place you call getText(). Therefore, the values in UserInput and UserInput2 are empty. that is the reason java.lang.numberformatexception is raising. make these changes and your application will work correctly.

  1. delete these two lines of code from onCreate() function.

    UserInput = findViewById(R.id.FirstNumber); UserInput2 = findViewById(R.id.SecondNumber);

  2. Create the following function outside of onCreate function

 public void getInput(){
                UserInput = findViewById(R.id.FirstNumber);
                UserInput2 = findViewById(R.id.SecondNumber);
            }
  1. Call this function inside every OnClickListener. like the following..
buttonSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               getInput();
               mText.setText("Answer = " + Double.toString((userinput - userinput2)));
            }
        });

I hope your problem is solved. ask me any question in comment below if you have any doubt

Abdulhakim Zeinu
  • 3,333
  • 1
  • 30
  • 37