任何人都可以看看下面的代码并指出明显的问题吗?editText
程序在留空时会出错,并且只有在所有信息都存在时才进行计算。目前程序在缺少文本时崩溃,这是为什么呢?
EditText editText1, editText2;
double numA, numB, numC;
TextView answer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calc);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
}
public void btnClick(View v)
{
if(editText1.getText().length()==0)
{
editText1.setError("please input text");
}{
if (editText2.getText().length()==0)
{
editText2.setError("please input text");
}
numA = new Double(editText1.getText().toString());
numB = new Double(editText2.getText().toString());
numC=(numA + numB); answer.setText(Double.toString(numC));
}
}}