0

不知道这里有什么问题。如果编辑文本点中没有任何内容,则会发生崩溃。如果它们都被填写,它会起作用。所以我的问题是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)
4

2 回答 2

1

由于异常已抛出:

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)

问题是:TextField您用来获取文本并解析 int。因为这个文本字段是空的(你应该记住,空与 非常不同NULL)。 TextField本身,不为空,但你得到的文本是空的("")。所以,Integer.parseInt在这种情况下会抛出异常。(不会像我们希望的那样自动返回 0)因此,在代码中,您必须将程序中try-catch的所有方法都用块包围Integer.parseInt,以防止用户进行一些愚蠢的输入。

int chol = 0;
int hdl = 0;
int age = 0;
try {

                    age = Integer.parseInt(selectedAge.toString());
                    chol = Integer.parseInt(cholEdit.getText().toString());
                    hdl = Integer.parseInt(hdlEdit.getText().toString());

}

catch (Exception e) {
     chol = 0;
     hdl = 0;
     age = 0;
}

这是您使用Integer.parseIntDouble.parseDouble...时的最佳模式

希望这可以帮助你:)

于 2013-04-23T02:46:06.260 回答
1

而不是检查“!= null”,您应该检查isNullOrEmpty(编写这样一个方法来检查“string == null || string.isEmpty()”,因为显然那些toString方法返回空字符串(“”)和 Integer.parseInt barfs 在空字符串上的作用与在字符串“hello”上的作用相同。

于 2013-04-23T02:39:34.147 回答