我在Android Studio上創建了一個簡單的計算器應用程序,並且遇到了我無法弄清的這個問題。我的計算器使用兩個單選按鈕來計算和兩個編輯文本字段以獲取用戶的數字。一切工作的計算去,但是當我運行該程序,並單擊兩個編輯文本字段中的任何一個單選按鈕,我的應用程序崩潰。另外,如果將其中一個編輯文本字段留空,然後單擊其中一個單選按鈕進行計算,它也會崩潰。另外,我希望程序在發生這些實例時不做任何事情,直到兩個編輯文本字段中有數字才能進行適當的計算。有人可以請解釋我做錯了什麼,以及我需要做什麼來解決這個問題。謝謝!單擊按鈕時,單選按鈕崩潰
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("");
}
我的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)
我不這麼認爲,因爲它與onClick按鈕有關,這是單選按鈕。 (java.lang.IllegalStateException:無法爲android:onClick執行方法)。另外,我希望程序在運行到沒有填充編輯文本字段的兩個實例中時不做任何事情 –