I am creating a simple calculator application on Android Studio, and I have run into this one problem I just cannot figure out. My calculator uses two radio buttons in order to calculate and two edit-text fields to get the numbers from the user. Everything works as for calculations goes, but when I run the program and click either one of the radio buttons with nothing in the two edit-text fields my app crashes. In addition, if leave either one of the edit-text field blank and then click either one of the radio-button to do the calculation it also crashes. Also, I want the program to do nothing when those instances occur until there are numbers in the the two edit-text fields to do a proper calculation. Can someone please explain to what is it that I am doing wrong, and what I need to do to fix this problem. Thank You!
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onRadioButtonClicked(View v) {
//this links the elements between the content on the screen and code together
RadioGroup buttonGrouper = (RadioGroup) findViewById(R.id.buttonGrouper);
RadioButton discountOffButton = (RadioButton) findViewById(R.id.discountOffButton);
RadioButton percentageOfButton = (RadioButton) findViewById(R.id.percentageOfButton);
EditText originalAmtInput = (EditText) findViewById(R.id.originalAmtInput);
EditText percentageAmtInput = (EditText) findViewById(R.id.percentageAmtInput);
TextView totalAmt = (TextView) findViewById(R.id.totalAmt);
//calculation initializations and formulas
float amount = Float.parseFloat(originalAmtInput.getText().toString());
float percentage = Float.parseFloat(percentageAmtInput.getText().toString());
float decimal = percentage / 100;
float total = amount * decimal;
float discountTotal = amount - total;
//error checks variables
String amountInput = originalAmtInput.getText().toString();
String percentageInput = percentageAmtInput.getText().toString();
boolean discountChecked = buttonGrouper.getCheckedRadioButtonId() == discountOffButton.getId();
boolean percentageChecked = buttonGrouper.getCheckedRadioButtonId() == percentageOfButton.getId();
if (discountChecked && !amountInput.isEmpty() && !percentageInput.isEmpty()) {
totalAmt.setText(Float.toString(discountTotal));
totalAmt.setText(String.format("$%.2f", discountTotal));
}
else if (percentageChecked && !amountInput.isEmpty() && !percentageInput.isEmpty()) {
totalAmt.setText(Float.toString(total));
totalAmt.setText(String.format("%.2f", total));
}
else {
totalAmt.setText("");
}
My Logcat:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.michaelzheng.percentagecalculatorapp, PID: 12575
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4780)
at android.widget.CompoundButton.performClick(CompoundButton.java:120)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4780)
at android.widget.CompoundButton.performClick(CompoundButton.java:120)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NumberFormatException: Invalid float: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseFloat(StringToReal.java:308)
at java.lang.Float.parseFloat(Float.java:306)
at com.example.michaelzheng.percentagecalculatorapp.MainActivity.onRadioButtonClicked(MainActivity.java:32)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4780)
at android.widget.CompoundButton.performClick(CompoundButton.java:120)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)