Java 为什么它显示即使在结果编辑文本字段中将int转换为string后,应用程序仍然被强制关闭?
实际上我正在开发一个简单的计算器。用户必须在两个编辑文本字段中输入两个值。我希望结果显示在第三个编辑文本上。我还将编辑文本字段的输入类型设置为“number”。可能我只做了加法部分。在执行测试时,它显示应用程序被强制关闭…我仍然不明白我的代码哪里出错了Java 为什么它显示即使在结果编辑文本字段中将int转换为string后,应用程序仍然被强制关闭?,java,android,xml,Java,Android,Xml,实际上我正在开发一个简单的计算器。用户必须在两个编辑文本字段中输入两个值。我希望结果显示在第三个编辑文本上。我还将编辑文本字段的输入类型设置为“number”。可能我只做了加法部分。在执行测试时,它显示应用程序被强制关闭…我仍然不明白我的代码哪里出错了 public class MainActivity extends Activity { public int a; public int b; @Override protected void onCreate
public class MainActivity extends Activity {
public int a;
public int b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText ed1=(EditText)findViewById(R.id.editText1);
final EditText ed2=(EditText)findViewById(R.id.editText2);
final EditText ed3=(EditText)findViewById(R.id.editText3);
Button b1=(Button)findViewById(R.id.button1);
Button b2=(Button)findViewById(R.id.button2);
Button b3=(Button)findViewById(R.id.button3);
Button b4=(Button)findViewById(R.id.button4);
Button b5=(Button)findViewById(R.id.button5);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
a = Integer.parseInt(ed1.getText().toString());
b = Integer.parseInt(ed2.getText().toString());
if(a!=0||b!=0)
{
int c=a+b;
ed3.setText(String.valueOf(c));
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
为什么不使用TextView而不是第三个EditText,对TextView使用相同的setText()方法,对强制关闭使用相同的setText()方法?OnCreateOptions菜单()方法中一定有错误 检查您是否在清单中添加了活动
也要具体说明您遇到了什么错误。日志中出现了什么错误该问题可能是因为您试图将空字符串转换为整数,请尝试在解析为int valuethnx buddy@kapsym进行响应之前添加一个空检查,但没有更多错误。错误被纠正了。只是“ed3.setText(String.valueOf(c));”不起作用。应该使用ed3.setText(Integer.toString(c));首先,我已经测试了你的代码,一切正常,但有两种情况可以调用强制关闭应用程序(例如,文本而不是数字)