不幸的是,由于文本中没有数据输入,我的应用程序停止了

my app stopped unfortunately due to no data entry into text

我在 android studio 上构建了一个 android 应用程序,不幸的是,如果我将文本留空且没有数字,它每次都会停止,

当我没有在短信中输入任何号码时,logcat 向我显示了这条消息: 编舞:跳过95帧!应用程序可能在其主线程上做了太多工作

这是我的等式:

public void onButtonClick(View v) {


    float n1, n2, n3, n4;
    double result1, result2;

    EditText e1 = (EditText) findViewById(R.id.Bwt);
    EditText e2 = (EditText) findViewById(R.id.Hit);
    TextView t1 = (TextView) findViewById(R.id.ibw);
    TextView t2 = (TextView) findViewById(R.id.abw);
    n1 = Float.parseFloat(e1.getText().toString());
    n2 = Float.parseFloat(e2.getText().toString());
    n3 = Float.parseFloat(Gender1.getText().toString());
    n4 = Float.parseFloat(Gender2.getText().toString());
    result1 = n3+(n4*((n2*0.3937007874)-60)) ;
    result2 = result1+(0.4*(n1-result1)) ;
    t1.setText(Double.toString(result1));
    DecimalFormat fresult = new DecimalFormat("#.000");
    String finaresult = fresult.format(result2);
    t2.setText(String.valueOf(finaresult));

}

试试这个:

public void onButtonClick(View v) {

    float n1, n2, n3, n4;
    double result1, result2;

    EditText e1 = (EditText) findViewById(R.id.Bwt);
    EditText e2 = (EditText) findViewById(R.id.Hit);
    TextView t1 = (TextView) findViewById(R.id.ibw);
    TextView t2 = (TextView) findViewById(R.id.abw);
    try {
        n1 = Float.parseFloat(e1.getText().toString());
        n2 = Float.parseFloat(e2.getText().toString());
        n3 = Float.parseFloat(Gender1.getText().toString());
        n4 = Float.parseFloat(Gender2.getText().toString());
    } catch (NumberFormatException e) {
        // Show message or something else
        return;
    }
    result1 = n3+(n4*((n2*0.3937007874)-60)) ;
    ...

}

您必须在使用用户输入调用 Float.parseFloat()Integer.parseInt() 等时捕获 NumberFormatException。