user2621649
user2621649

Reputation:

Adding a +/- Button to my calculator ? - Android

Okay So I want to add this +/- button to change the sign of the number currently being shown in the result window (An EditText Component)

Here's the OnClick Funciton at the button press of the button :

public void onClickListenerPM(View v) {
    vibrator.vibrate(30);
    if (press == '=') {
        onClickListenerReset(buttonReset);
    }

    double masag = -1 * Double.parseDouble(EditTextMsg);
    String s = Double.toString(masag);
    editText.setText(s);
}

What I've done is Taken the string from the EditText view ---> made it into a double ---> Reverse , Put that into a string again , so that the sign of whatever number is reversed. ---> Show it in the edittext view.

But whenever I click the button, the app force Closes ...

What's going wrong ? I think the logic is correct but most probably the conversions are causing the function to malfunction. (Not sure though)

Can you spot anything that might be causing this ?

(This is defined in the activity class obviously...)

EDIT : Answer : EditTextMsg = editText.getText().toString();

forgot to add this -.-

Upvotes: 0

Views: 1117

Answers (1)

Trinimon
Trinimon

Reputation: 13967

You haven't posted how you extracted the EditText String value. Did you use ...

EditText edit = (EditText)findViewById(R.id.edit_text_id);
String editTextMsg = edit.getText().toString();

... toString is important here! And I would rather use Double.toString() than toString(double):

Double masag = -1 * Double.valueOf(editTextMsg);
String s = masag.toString();

here. Hope this helps ...

Cheers!

Upvotes: 2

Related Questions