程序崩溃与空文本字段

时间:2013-07-21 22:58:47

标签: java android text if-statement textfield

我需要帮助才能修复我的代码。我认为它很简单(可能是)但我无法得到它。我有一个简单的添加计算器。它工作正常,但如果我将1个或两个Number文本字段留空,程序崩溃。

我有我的if声明,但显然我并没有告诉它做正确的事情。

public class MainActivity extends Activity {
Double firstNum, secondNum, answerNum;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText first = (EditText) findViewById(R.id.txtFirst);
    final EditText second = (EditText) findViewById(R.id.txtSecond);


    final TextView answer = (TextView) findViewById(R.id.txtAnswer);



    Button calc = (Button) findViewById(R.id.btnCalc);
    calc.setOnClickListener(new OnClickListener() {



        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            // convert pulled info to double using variable names
            firstNum = Double.parseDouble(first.getText().toString());
            secondNum = Double.parseDouble(second.getText().toString());

            if (first == null || second == null)
            {
                Toast.makeText(MainActivity.this, "Please Enter value", Toast.LENGTH_SHORT).show();
            }
            else {

            // add numbers
            answerNum = (firstNum + secondNum);
            //set format
            DecimalFormat total = new DecimalFormat ("###,###,###.##");
            answer.setText("Answer is " + total.format(answerNum));
            }
        }
    });
}
}

2 个答案:

答案 0 :(得分:2)

由于行Double.parseDouble(second.getText().toString()),您有异常,它无法将空字符串解析为double,因此您应该添加一些验证代码。

答案 1 :(得分:0)

如果您想要验证edittext,请尝试以下代码,它将正常工作。

public class MainActivity extends Activity {
double firstNum = 0;
double secondNum = 0, answerNum;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText first = (EditText) findViewById(R.id.editText1);
    final EditText second = (EditText) findViewById(R.id.editText2);

    final TextView answer = (TextView) findViewById(R.id.textView1);

    Button calc = (Button) findViewById(R.id.button1);
    calc.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


            if (first.getText().toString().equals("")
                    || second.getText().toString().equals("")) {
                Toast.makeText(MainActivity.this, "Please Enter value",
                        Toast.LENGTH_SHORT).show();
            } else {
                firstNum = Double.parseDouble(first.getText().toString());
                secondNum = Double.parseDouble(second.getText().toString());
                answerNum = (firstNum + secondNum);
                DecimalFormat total = new DecimalFormat("###,###,###.##");
                answer.setText("Answer is " + total.format(answerNum));
            }
        }
    });
}
}