我正在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)
答案 0 :(得分:0)
如果EditText为空,也许你应该制作Toast,告诉用户在EditText中输入内容,我认为应该解决问题?
答案 1 :(得分:0)
好的,经过很长一段时间对此进行研究后,我终于通过阅读这两篇文章解决了我的问题。
第一篇帖子是: App Crashes When TextField is Empty
第二篇帖子是: If EditText.getText().ToString() == "" dont work;
我所做的是为我的金额和百分比变量初始化添加一个条件
之前:
//calculation initialization and formulas
float amount = Float.parseFloat(originalAmtInput.getText().toString());
float percentage =Float.parseFloat(percentageAmtInput.getText().toString());
之后:
//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());
"".equals(originalAmtInput.getText().toString()) ? 0 : Float.parseFloat(originalAmtInput.getText().toString())
的含义是它检查originalAmtInput编辑文本字段是否为空,方法是将其等于""。因此,如果它为空,则将值设置为0.否则,该值将设置为用户的输入数字。