不知道这里有什么问题。如果编辑文本点中没有任何内容,则会发生崩溃。如果它们都被填写,它会起作用。所以我的问题是if
语句检查。if
即使编辑文本为空,它也会进入语句。它应该跳到else
并记录并非所有字段都已完成。
问题就在这里(我认为)-即使不应该这样做,它也会跳入循环:
//Calculate Risk Score Button
Button calcButton = (Button) findViewById(R.id.calcButton);
calcButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Create and set values
int smoker = 0;
int medicated = 0;
char gender = 'm';
String selectedAge = (String) ageSpin.getSelectedItem();
Log.i("TEST", "Before if");
if (selectedAge != null
&& cholEdit.getText().toString() != null
&& hdlEdit.getText().toString() != null
&& sbpEdit.getText().toString() != null
&& genderGroup.getCheckedRadioButtonId() != -1
&& smokerGroup.getCheckedRadioButtonId() != -1
&& medsGroup.getCheckedRadioButtonId() != -1) {
Log.i("TEST", "After if");
// store values
int age = Integer.parseInt(selectedAge.toString());
int chol = Integer.parseInt(cholEdit.getText().toString());
int hdl = Integer.parseInt(hdlEdit.getText().toString());
int sbp = Integer.parseInt(sbpEdit.getText().toString());
int genderId = genderGroup.getCheckedRadioButtonId();
int smokeId = smokerGroup.getCheckedRadioButtonId();
int medsId = medsGroup.getCheckedRadioButtonId();
// Set gender, smoker and if medicated values
if (genderId == R.id.maleR)
gender = 'm';
else if (genderId == R.id.femaleR)
gender = 'f';
if (smokeId == R.id.yesR)
smoker = 1;
else if (smokeId == R.id.noR)
smoker = 0;
if (medsId == R.id.yesR2)
medicated = 1;
else if (medsId == R.id.yesR2)
medicated = 0;
// Calculate Answer and Print
String answer = calc.calculateRiskScore(age, gender, chol,
smoker, hdl, sbp, medicated);
SharedPreferences settings = getSharedPreferences(
PREF_FILE, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("age", age);
editor.putInt("chol", chol);
editor.putInt("hdl", hdl);
editor.putInt("sbp", sbp);
editor.putInt("gender", genderId);
editor.putInt("smoker", smoker);
editor.putInt("med", medicated);
editor.putString("risk", answer);
editor.commit();
displayRisk(answer);
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"Not all fields are completed", Toast.LENGTH_SHORT);
toast.show();
}
}
});
这是 logcat 错误。我明白错误在说什么,但它不应该出现在if
声明中:
04-22 21:26:43.920: I/TEST(22544): Before if
04-22 21:26:43.920: I/TEST(22544): After if
04-22 21:26:43.920: W/dalvikvm(22544): threadid=1: thread exiting with uncaught exception (group=0x41634438)
04-22 21:26:43.930: E/AndroidRuntime(22544): FATAL EXCEPTION: main
04-22 21:26:43.930: E/AndroidRuntime(22544): java.lang.NumberFormatException: Invalid int: ""
04-22 21:26:43.930: E/AndroidRuntime(22544): at java.lang.Integer.invalidInt(Integer.java:138)
04-22 21:26:43.930: E/AndroidRuntime(22544): at java.lang.Integer.parseInt(Integer.java:359)
04-22 21:26:43.930: E/AndroidRuntime(22544): at java.lang.Integer.parseInt(Integer.java:332)
04-22 21:26:43.930: E/AndroidRuntime(22544): at edu.auburn.eng.csse.comp3710.group04.CardiovascularActivity$1.onClick(CardiovascularActivity.java:83)
04-22 21:26:43.930: E/AndroidRuntime(22544): at android.view.View.performClick(View.java:4198)
04-22 21:26:43.930: E/AndroidRuntime(22544): at android.view.View$PerformClick.run(View.java:17164)
04-22 21:26:43.930: E/AndroidRuntime(22544): at android.os.Handler.handleCallback(Handler.java:615)
04-22 21:26:43.930: E/AndroidRuntime(22544): at android.os.Handler.dispatchMessage(Handler.java:92)
04-22 21:26:43.930: E/AndroidRuntime(22544): at android.os.Looper.loop(Looper.java:137)
04-22 21:26:43.930: E/AndroidRuntime(22544): at android.app.ActivityThread.main(ActivityThread.java:4918)
04-22 21:26:43.930: E/AndroidRuntime(22544): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 21:26:43.930: E/AndroidRuntime(22544): at java.lang.reflect.Method.invoke(Method.java:511)
04-22 21:26:43.930: E/AndroidRuntime(22544): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
04-22 21:26:43.930: E/AndroidRuntime(22544): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
04-22 21:26:43.930: E/AndroidRuntime(22544): at dalvik.system.NativeStart.main(Native Method)