TextView没有文本时如何返回方法?

时间:2016-08-24 19:38:10

标签: java android methods return

我想这样做,当editText字段为空时,不显示任何结果而不是应用程序崩溃。

我在这里阅读了这个结果:How to break out or exit a method in Java?

以及基于该数字是负数还是正数的其他一些结果。我已经在这里工作了大约一个小时,我还在扯皮。我是新手,我想我一定不要把if语句放在正确的位置?

这是代码。任何正确方向的指针都会有所帮助。谢谢。

[Facebook\WebDriver\Exception\UnexpectedAlertOpenException] Unexpected modal
dialog (text: Fallback login. Enter password): Fallback login. Enter
password

这是logCat错误。你们快!

public class incomePage extends AppCompatActivity {
    EditText perYearAmount;
    EditText perMonthAmount;
    EditText perWeekAmount;
    Button totalButton;
    TextView addResult;


    double year, month, week, sum;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_income_page);
        perYearAmount = (EditText) findViewById(R.id.perYearAmount);
        perMonthAmount = (EditText) findViewById(R.id.perMonthAmount);
        perWeekAmount = (EditText) findViewById(R.id.perWeekAmount);
        totalButton = (Button) findViewById(R.id.totalButton);
        addResult = (TextView) findViewById(R.id.totalMonthlyIncome);


        totalButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                year = Double.parseDouble(perYearAmount.getText().toString());
                month = Double.parseDouble(perMonthAmount.getText().toString());
                week = Double.parseDouble(perWeekAmount.getText().toString());
                sum = year + month + week;
                addResult.setText(Double.toString(sum));
                if (perYearAmount.getText().toString().equals("")) {
                    return;
                }
                if (perMonthAmount.getText().toString().equals("")) {
                    return;
                }
                if (perWeekAmount.getText().toString().equals("")) {
                    return;
                }
                if (totalButton.getText().toString().equals("")) {
                    return;
                }
                if (addResult.getText().toString().equals("")) {
                    return;
                }

            }
        });

    }
}

3 个答案:

答案 0 :(得分:2)

您需要检查EditText中是否有中的文字尝试将(可能的)不存在的文本解析为数值。例如

String yearString = perYearAmount.getText().toString();

if (!"".equals(yearString)) {
    year = Double.parseDouble(yearString);
}

请注意,我创建了一个临时变量yearString,以避免输入和执行perYearAmount.getText().toString()两次。

答案 1 :(得分:0)

使用TextUtils。

totalButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        if (TextUtils.isEmpty(perYearAmount.getText().toString())||
            TextUtils.isEmpty(perMonthAmount.getText().toString())||
            TextUtils.isEmpty(perWeekAmount.getText().toString()))   {

                addResult.setText(""); // if any of the fields is empty, add nothing to textview

        } else {
            year = Double.parseDouble(perYearAmount.getText().toString());
            month = Double.parseDouble(perMonthAmount.getText().toString());
            week = Double.parseDouble(perWeekAmount.getText().toString());
            sum = year + month + week;
            addResult.setText(Double.toString(sum));
        }
        }
    });

答案 2 :(得分:0)

崩溃是由NumberFormatException引起的,当参数不是可解析的double时,Double.parseDouble()方法会抛出该崩溃。在错误日志中,这显示为空String

java.lang.NumberFormatException: Invalid double: ""

要解决此问题,您可以在try / catch块中包围任何Double.parseDouble()次调用:

try {
    Double.parseDouble(anEditText.getText().toString());
} catch (NumberFormatException nfe) {
    //anEditText does not contain a parsable Double
}

修改 要使用您的代码创建示例:

double year = 0;
double month = 0;
try {
    year = Double.parseDouble(perYearAmount.getText().toString());
    month = Double.parseDouble(perMonthAmount.getText().toString());
} catch (NumberFormatException nfe) {
    Toast.makeText(this, "Please enter a valid number and try again", Toast.LENGTH_SHORT).show();
    return;
}

double sum = year + month;
//etc...