错误java.lang.NumberFormatException:对于输入字符串,程序崩溃

时间:2019-04-21 09:11:47

标签: java android

我实现了一个应用程序,该应用程序从用户那里读取数据并返回一条消息,但是在尝试打开活动时会发生以下错误:

2019-04-21 10:03:27.179 4338-4338/com.example.mymedicare E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.mymedicare, PID: 4338
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mymedicare/com.example.mymedicare.enteringReadings}: java.lang.NumberFormatException: For input string: ""
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: java.lang.NumberFormatException: For input string: ""
        at java.lang.Integer.parseInt(Integer.java:620)
        at java.lang.Integer.parseInt(Integer.java:643)
        at com.example.mymedicare.enteringReadings.onCreate(enteringReadings.java:28)
        at android.app.Activity.performCreate(Activity.java:6975)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770
public class enteringReadings extends AppCompatActivity {

private MyDBHelper db;
//declares values
private Button Enter;
private EditText Temprature;
private EditText lowerBlood;
private EditText higherBlood;
private EditText heartBeat;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_entering_readings);

    //Reads values as integers
    Temprature = findViewById(R.id.Temprature);
    int temprature =Integer.parseInt( Temprature.getText().toString());
    lowerBlood = findViewById(R.id.lowerBlood);
    int Lowerblood =Integer.parseInt( lowerBlood.getText().toString());
    higherBlood = findViewById(R.id.higherBlood);
    int Higherblood =Integer.parseInt( higherBlood.getText().toString());
    heartBeat = findViewById(R.id.heartBeat);
    int Heartbeat =Integer.parseInt( heartBeat.getText().toString());

    Button Enter = findViewById(R.id.Enter);
    db = new MyDBHelper(this);

    Enter.setOnClickListener((View.OnClickListener) this);

    if (temprature <= 37 && Lowerblood <=80 && Higherblood <=120 && Heartbeat <= 72 );
    Toast.makeText(getApplicationContext(),"Your readings are fine no action required",Toast.LENGTH_LONG);
}
}

我希望该应用允许用户输入读数,但我还没有实现其他两种情况,但是在尝试运行该应用后发现了此错误。

4 个答案:

答案 0 :(得分:0)

function changeText1(param) { var val = param.getAttribute('value'); // ATTRIBUTE-NAME param.innerHTML = val; }返回空字符串。空字符串不能强制转换为Temprature.getText()。 在代码中添加Integer块以处理try-catch

NumberFormatException

答案 1 :(得分:0)

如您所见:

ComponentInfo{com.example.mymedicare/com.example.mymedicare.enteringReadings}: java.lang.NumberFormatException: For input string: ""

他告诉您正在尝试转换一个空字符串,例如,应该在开始时为int赋值。

Temprature = findViewById(R.id.Temprature);
    int temprature = 0;
    temprature =Integer.parseInt( Temprature.getText().toString());

因此,即使editText为空,系统也会传递0。

否则,如果仅在用户选择一个时才需要传递值,则可以通过以下方式做到这一点:

if (!Temprature.getText().toString().equals(""){
        temprature =Integer.parseInt( Temprature.getText().toString());

}else{
        Toast.makeText(context,"Please, select a value", 
            Toast.LENGTH_SHORT).show();
}

答案 2 :(得分:0)

您应该在单击侦听器上的任何按钮上进行操作,首先应检查是否有任何框为空,如果为空,则不能将空字符串转换为数字,因为错误表明。

答案 3 :(得分:0)

public class enteringReadings extends AppCompatActivity {

private MyDBHelper db;
//declares values
private Button Enter;
private EditText Temprature;
private EditText lowerBlood;
private EditText higherBlood;
private EditText heartBeat;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_entering_readings);
    Temprature = findViewById(R.id.Temprature);
    lowerBlood = findViewById(R.id.lowerBlood);
    higherBlood = findViewById(R.id.higherBlood);
    heartBeat = findViewById(R.id.heartBeat);

    //Reads values as integers


    Button Enter = findViewById(R.id.Enter);
    db = new MyDBHelper(this);

    //Enter.setOnClickListener((View.OnClickListener) this);
    Enter.setOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            int temprature = 0;
            try {
                temprature = Integer.parseInt(Temprature.getText().toString());
            } catch (NumberFormatException e) {
                // Log error, change value of temperature, or do nothing
            }

            int Lowerblood = 0;
            try {
                Lowerblood = Integer.parseInt(lowerBlood.getText().toString());
            } catch (NumberFormatException e) {
                // Log error, change value of lowerBlood, or do nothing
            }

            int Higherblood = 0;
            try {
                Higherblood = Integer.parseInt(higherBlood.getText().toString());
            } catch (NumberFormatException e) {
                // Log error, change value of higherBlood, or do nothing
            }

            int Heartbeat = 0;
            try {
                Heartbeat = Integer.parseInt(heartBeat.getText().toString());
            } catch (NumberFormatException e) {
                // Log error, change value of heartBeat, or do nothing
            }
            if (temprature <= 37 && Lowerblood <= 80 && Higherblood <= 120 && Heartbeat <= 72) ;
            Toast.makeText(getApplicationContext(), "Your readings are fine no action required", Toast.LENGTH_LONG).show();


        }

    });
}

}