java - 我的应用程序抛出运行时异常并崩溃。我必须添加一些东西吗?
问题描述
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;
解决方案
在displayMessage
方法中,写
result.setText(String.valueOf(message));
您将计算出的整数值设置为 TextView 但此值应为String。如果您设置为整数,则表示您设置了 String Resource ID likes R.string.app_name
。
推荐阅读
- apache-spark - groupBy 的 Java Spark 替代方案
- xcode - 如何在 Xcode 中隐藏故事板概览?
- python - Python - 循环中的增量变量供以后调用
- drupal - 如何定位 Drupal 视图中的第一个项目?
- java - java - 如何在java中的映射中添加有序对作为键和值?
- reactjs - mui-datatables 从外部过滤
- elasticsearch - kibana 上的时间戳格式错误
- python - 如何使用条件/范围对相似的数字进行分组
- azure - 在 azure 中运行 powershell 脚本时无法获取租户组织的令牌
- reactjs - React / Neovis.js:无法构造“WebSocket”:可能无法从通过 HTTPS 加载的页面启动不安全的 WebSocket 连接