Java Android empty editText正在破坏我的程序

Java Android empty editText正在破坏我的程序,java,android,Java,Android,第一次使用Java时,我正试图为我工作的餐厅的同事创建一个简单的提示计算器,但当我将其中一个editText字段留空时,程序崩溃 主要活动: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); totalTipsInput = (EditText)

第一次使用Java时,我正试图为我工作的餐厅的同事创建一个简单的提示计算器,但当我将其中一个editText字段留空时,程序崩溃

主要活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    totalTipsInput = (EditText) findViewById(R.id.totalTipsInput);
    waiter1Hours = (EditText) findViewById(R.id.waiter1Hours);
    waiter2Hours = (EditText) findViewById(R.id.waiter2Hours);
    waiter3Hours = (EditText) findViewById(R.id.waiter3Hours);
    waiter4Hours = (EditText) findViewById(R.id.waiter4Hours);

    tipsPerHourView = (TextView) findViewById(R.id.tipsPerHourView);
    totalHoursView = (TextView) findViewById(R.id.totalHoursView);
    barsCutView = (TextView) findViewById(R.id.barsCutView);

    waiter1Pay = (TextView) findViewById(R.id.waiter1Pay);
    waiter2Pay = (TextView) findViewById(R.id.waiter2Pay);
    waiter3Pay = (TextView) findViewById(R.id.waiter3Pay);
    waiter4Pay = (TextView) findViewById(R.id.waiter4Pay);
    taxDepositView = (TextView) findViewById(R.id.taxDepositView);

    Button calcBtn = (Button) findViewById(R.id.calcBtn);
    calcBtn.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View view) {

            double totalTips = Double.parseDouble(totalTipsInput.getText().toString());

            double cWaiter1Hours = Double.parseDouble(waiter1Hours.getText().toString());
            double cWaiter2Hours = Double.parseDouble(waiter2Hours.getText().toString());
            double cWaiter3Hours = Double.parseDouble(waiter3Hours.getText().toString());
            double cWaiter4Hours = Double.parseDouble(waiter4Hours.getText().toString());

            double resultTotalHours = cWaiter1Hours + cWaiter2Hours + cWaiter3Hours + cWaiter4Hours;
            double resultBarsCut = (totalTips * 7) / 100;
            double resultTaxDeposit = resultTotalHours * 3;
            double resultTipsPerHour = (totalTips - resultBarsCut - resultTaxDeposit) / resultTotalHours;

            double resultWaiter1Pay = cWaiter1Hours * resultTipsPerHour;
            double resultWaiter2Pay = cWaiter2Hours * resultTipsPerHour;
            double resultWaiter3Pay = cWaiter3Hours * resultTipsPerHour;
            double resultWaiter4Pay = cWaiter4Hours * resultTipsPerHour;

            totalHoursView.setText(Double.toString(resultTotalHours));
            tipsPerHourView.setText(Double.toString(resultTipsPerHour));
            barsCutView.setText(Double.toString(resultBarsCut));

            waiter1Pay.setText(Double.toString(resultWaiter1Pay));
            waiter2Pay.setText(Double.toString(resultWaiter2Pay));
            waiter3Pay.setText(Double.toString(resultWaiter3Pay));
            waiter4Pay.setText(Double.toString(resultWaiter4Pay));

            taxDepositView.setText(Double.toString(resultTaxDeposit));


        }
    });
}
尝试执行类似的操作,但出现了错误。长度:

                if (double totalTips = Double.parseDouble(totalTipsInput.getText().toString()).length() < 1 || totalTipsInput = null) {
                totalTips = 0
            } else {
                double totalTips = Double.parseDouble(totalTipsInput.getText().toString());
            }

您可以这样做以确保parseDouble的输入有效:

    double totalTips = 0;
    Editable totalString = totalTipsInput.getText();
    if(totalString.length() > 0){
        totalTips = Double.parseDouble(totalString.toString());
    }
如果totalTips字段可以接受用户输入,则需要确保他们只能输入有效的数字。懒惰的方法可能是在parseDouble周围放置一个try/catch,并处理可能有人输入无法解析为双精度的内容(即空字符串、字母、格式错误的数值)的情况


我建议不要相信用户总是在其余的侍者时间字段中输入有效值。在尝试分析输入之前,您可能希望对这些字段执行类似的检查。

在类中使用此方法:

 public static Double returnDouble(EditText editText)
    {
        try {
            if(editText.getText().toString().isEmpty())
            {
                return 0d;
            }
            else
            {
                return Double.parseDouble(editText.getText().toString());
            }
        } catch (NumberFormatException e) {

            return 0d;
        }

    }

您可以这样做:String tmpStr=totalTipsInput.getText.toString;double totalTips=tmpStr.legnth>0?Double.parseDoubletmpStr:0.0;对空字符串上的其他字段进行调整。Double.parseDouble异常?尝试ifwaiter1Hours.getText之类的操作=null double cWaiter1Hours=double.parseDoublewaiter1Hours.getText.toString@EditText上的Redman getText不会返回空值,至少在我的经验中它从未返回过空值。然后,他可能还可以使用和条件检查空文本。谢谢,这很有效!好@Kiskae,谢谢你的编辑。