我正在尝试使用 Eclipse 创建适用于 Android 的计算器。这是我的函数“cal”的代码,单击按钮时执行“cal”,但是单击按钮关闭应用程序,我尝试了另一个具有不同功能的按钮并且它工作正常。谁能指出我犯的错误?
public void cal( View view ){
EditText op = (EditText) findViewById(R.id.editText2);
EditText n1 = (EditText) findViewById(R.id.editText1);
EditText n2 = (EditText) findViewById(R.id.editText3);
EditText res = (EditText) findViewById(R.id.textView1);
String sn1 = n1.getText().toString();
String sn2 = n2.getText().toString();
String sres;
String sop;
int in1 = Integer.parseInt(sn1);
int in2 = Integer.parseInt(sn2);
int ires;
sop = op.getText().toString();
if(sop == "+"){
ires = in1 + in2;
sres = Integer.toString(ires);
res.setText(sres);
}
}
最佳答案
这也不异常(exception)。
我认为您忘记设置 Activity 布局导致您的应用程序强制关闭。
或
这里有问题
EditText res = (EditText) findViewById(R.id.textView1);
您确定布局中的 editText 名称是 textView1
吗?
如果是 textView,那么就这样做。
TextView res = (TextView) findViewById(R.id.textView1);
关于java - 用两个字符串计算,Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15952449/