当editText字段为空时
问题描述:
我已经看到了一些类似于我的其他标题,但是我的不同之处在于,输入的字符串被解析为双精度,只要所有的editText字段都被填充,它就可以工作没问题,但是如果一个字段留空,它会返回到最后一个活动!如果我诚实的话,logcat中的错误是模糊的,我认为它谈论的事实是没有意图在下一个活动,我已经实现,但它仍然崩溃的按钮,所以我不知道在哪里我错了。任何意见,将不胜感激,这里是我的代码和logcat的错误:当editText字段为空时
package com.example.siuni.mymedicare;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class weighinscreen extends ActionBarActivity {
private Button result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weighinscreen);
result = (Button) findViewById(R.id.button1);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText txtTempC = (EditText) findViewById(R.id.editText1);
String strC = txtTempC.getText().toString();
double TempC = Double.parseDouble(strC);//Parses the string as double
EditText txtTempF = (EditText) findViewById(R.id.editText2);
String strF = txtTempF.getText().toString();
double TempF = Double.parseDouble(strF););//Parses the string as double
EditText txtBPS = (EditText) findViewById(R.id.editText3);
String strBPS = txtBPS.getText().toString();
double BPS = Double.parseDouble(strBPS););//Parses the string as double
EditText txtBPD = (EditText) findViewById(R.id.editText4);
String strBPD = txtBPD.getText().toString();
double BPD = Double.parseDouble(strBPD););//Parses the string as double
EditText txtHeartR = (EditText) findViewById(R.id.editText5);
String strH = txtHeartR.getText().toString();
double HeartR = Double.parseDouble(strH););//Parses the string as double
if (Double.compare(TempC, Double.NaN) == 0) {//Should check the double to see if it null and produce
Toast.makeText(getApplicationContext(), "Please enter your temperature",
Toast.LENGTH_SHORT).show();
result.setEnabled(true);
} else if (Double.compare(TempF, Double.NaN) == 0){//Should check the double to see if it null and produce the toast
Toast.makeText(getApplicationContext(), "Please enter your temperature",
Toast.LENGTH_SHORT).show();
result.setEnabled(true);
} else if (Double.compare(BPS, Double.NaN) == 0) {//Should check the double to see if it null and produce the toast
Toast.makeText(getApplicationContext(), "Please enter your blood pressure",
Toast.LENGTH_LONG).show();
result.setEnabled(true);
} else if (Double.compare(BPD, Double.NaN) == 0) {//Should check the double to see if it null and produce the toast
Toast.makeText(getApplicationContext(), "Please enter your blood pressure",
Toast.LENGTH_LONG).show();
result.setEnabled(true);
} else if (Double.compare(HeartR, Double.NaN) == 0{//Should check the double to see if it null and produce the toast
Toast.makeText(getApplicationContext(), "Please enter your heart rate",
Toast.LENGTH_LONG).show();
result.setEnabled(true);
} else if (TempC <=36 && TempF <= 99 && BPS <= 100 && BPD <= 40 && HeartR <= 60) {//If the doubles are equal to the figures produces the toast
Toast.makeText(getApplicationContext(), "Please enter contact your GP",
Toast.LENGTH_LONG).show();
startActivity(new Intent(weighinclass.this, register.class));//moves on the next actvity
result.setEnabled(false);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
和错误:如果你的EditText提供空值
05-14 08:57:56.944 2392-2407/com.example.siuni.mymedicare W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-14 08:57:56.944 2392-2407/com.example.siuni.mymedicare W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6cc21c0, error=EGL_SUCCESS
答
应用程序会崩溃。
您在的EditText中的onClick & 得到的字符串(有时空)试图解析它。
它会在您的代码throw a NumberFormatException
&你是not using any Exception handling mechanism
。所以显然你的应用程序会崩溃。
提供一些检查,像
if(!strH.equals("")){
double HeartR = Double.parseDouble(strH););
}
else{
Toast.makeText(getApplicationContext(), "Please enter your Heart Rate",Toast.LENGTH_LONG).show();
}
注:所提供的日志不与您的问题有关。正确的日志会是这样的
我试着按照你的建议来实现这样的检查,没有明显的错误。这是在模拟器和联系7! –
请尝试获得logcat输出 –
由于我植入了代码,这是来自logcat的错误: java.lang.NumberFormatException:无效的双倍:“” –