【发布时间】:2015-05-14 09:34:59
【问题描述】:
我已经看到了一些与我的类似的其他标题,但我的不同之处在于输入的字符串被解析为双精度,只要填充了所有 editText 字段,它就可以正常工作,但如果一个字段是留空它会回到最后一个活动!老实说,logcat 中的错误是模糊的,我认为它在谈论这样一个事实,即我已经实现的下一个活动的按钮没有意图,但它仍然崩溃,所以我不确定在哪里我错了。任何建议将不胜感激,这是我的代码和 logcat 错误:
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);
}
}
还有错误:
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
【问题讨论】:
标签: android android-intent android-activity android-logcat