首页 > 解决方案 > 我的应用程序抛出运行时异常并崩溃。我必须添加一些东西吗?

问题描述

 public void submit(View view){
    EditText editHeight = (EditText)findViewById(R.id.height_text_view);
    String height = editHeight.getText().toString();
    int finalHeight = Integer.parseInt(height);

    EditText editField = (EditText)findViewById(R.id.weight_text_view);
    String weight = editField.getText().toString();
    int finalWeight =Integer.parseInt(weight);

    int calc = calculate(finalHeight, finalWeight);
    displayMessage(calc);

}

public void displayMessage(int message){

    TextView result = (TextView) findViewById(R.id.result_text_view);
    result.setText(message);
}
private int calculate(int height, int weight){

    return weight * height;

我是android新手,所以请给我详细的解决方案信息。这是我的错误详细信息:

标签: javaandroidruntime-errorillegalstateexception

解决方案


displayMessage方法中,写

result.setText(String.valueOf(message));

您将计算出的整数值设置为 TextView 但此值应为String。如果您设置为整数,则表示您设置了 String Resource ID likes R.string.app_name


推荐阅读