简体   繁体   中英

Radio Button crashes my app when clicking it

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) 

如果EditText为空,也许您应该做Toast告诉用户在EditText中键入一些内容,我认为这应该可以解决问题?

Ok, after a long time doing research on this, I finally have the solution to my problem by reading these two posts.

1st Post is: App Crashes When TextField is Empty

2nd Post is: If EditText.getText().ToString() == "" dont work;

What I did is add a condition to my amount and percentage variable initialization

BEFORE:

 //calculation initialization and formulas
float amount = Float.parseFloat(originalAmtInput.getText().toString());
float percentage =Float.parseFloat(percentageAmtInput.getText().toString());

AFTER:

 //calculation initializations and formulas  (note: ? = set to AND : = else set to)
    float amount = "".equals(originalAmtInput.getText().toString()) ? 0 : Float.parseFloat(originalAmtInput.getText().toString());
    float percentage = "".equals(percentageAmtInput.getText().toString()) ? 0 : Float.parseFloat(percentageAmtInput.getText().toString());

What "".equals(originalAmtInput.getText().toString()) ? 0 : Float.parseFloat(originalAmtInput.getText().toString()) "".equals(originalAmtInput.getText().toString()) ? 0 : Float.parseFloat(originalAmtInput.getText().toString()) means is that it checks if the originalAmtInput edit-text field is empty by equaling it to "". So if it is empty it will set the value to 0. Else the value will be set to the user's input numbers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM