所以基本上我有问题。当用户在未输入值的情况下按下计算按钮(我正在制作计算器)时,应用程序会崩溃。我希望它显示一条消息,但我不知道如何。(更新)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.waist2height); {
//h is cm edittext
final EditText h = (EditText)findViewById(R.id.editText2);
final EditText w = (EditText)findViewById(R.id.editText3);
final EditText r = (EditText)findViewById(R.id.textView4);
});
calculate.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
float height=0;
float weight=0;
float result=0;
result = (float) (( weight / height ) + 0.005) ;
if ((h.getText().toString()).equals(""))
{
Toast.makeText(getApplicationContext(),"Please enter your height", Toast.LENGTH_SHORT).show();
float height = Float.parseFloat(h.getText().toString());
}
if ((w.getText().toString()).equals(""))
{
Toast.makeText(getApplicationContext(),"Please enter your weight", Toast.LENGTH_SHORT).show();
float weight = Float.parseFloat(w.getText().toString());
}
r.setText(String.valueOf(result));
}
下面的代码是我遇到问题的地方
if ((h.getText().toString()).equals(""))
{
Toast.makeText(getApplicationContext(),"Please enter your height", Toast.LENGTH_SHORT).show();
float height = Float.parseFloat(h.getText().toString());
}
if ((w.getText().toString()).equals(""))
{
Toast.makeText(getApplicationContext(),"Please enter your weight", Toast.LENGTH_SHORT).show();
float weight = Float.parseFloat(w.getText().toString());
}
这是我能想到的最好的,但它不起作用。
这是 LogCat(更新)
09-20 18:50:38.509: E/AndroidRuntime(29769): FATAL EXCEPTION: main
09-20 18:50:38.509: E/AndroidRuntime(29769): Process: com.NovaPlex.personalfitnessfree, PID: 29769
09-20 18:50:38.509: E/AndroidRuntime(29769): java.lang.NumberFormatException: Invalid float: ""
09-20 18:50:38.509: E/AndroidRuntime(29769): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
09-20 18:50:38.509: E/AndroidRuntime(29769): at java.lang.StringToReal.parseFloat(StringToReal.java:308)
09-20 18:50:38.509: E/AndroidRuntime(29769): at java.lang.Float.parseFloat(Float.java:306)
09-20 18:50:38.509: E/AndroidRuntime(29769): at com.NovaPlex.personalfitnessfree.WH$2.onClick(WH.java:80)
09-20 18:50:38.509: E/AndroidRuntime(29769): at android.view.View.performClick(View.java:4780)
09-20 18:50:38.509: E/AndroidRuntime(29769): at android.view.View$PerformClick.run(View.java:19866)
09-20 18:50:38.509: E/AndroidRuntime(29769): at android.os.Handler.handleCallback(Handler.java:739)
09-20 18:50:38.509: E/AndroidRuntime(29769): at android.os.Handler.dispatchMessage(Handler.java:95)
09-20 18:50:38.509: E/AndroidRuntime(29769): at android.os.Looper.loop(Looper.java:135)
09-20 18:50:38.509: E/AndroidRuntime(29769): at android.app.ActivityThread.main(ActivityThread.java:5254)
09-20 18:50:38.509: E/AndroidRuntime(29769): at java.lang.reflect.Method.invoke(Native Method)
09-20 18:50:38.509: E/AndroidRuntime(29769): at java.lang.reflect.Method.invoke(Method.java:372)
09-20 18:50:38.509: E/AndroidRuntime(29769): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
09-20 18:50:38.509: E/AndroidRuntime(29769): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)