How to stop the app crashing when String is empty

自作多情 提交于 2019-12-25 04:08:22

问题


The app crash when there is no text ! I don't have what 'if' to put to fix that ! Thanks you

enter  TextView TotalTextView;
EditText EnterPercentage;
EditText EnterPrice;

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


    TotalTextView = (TextView) findViewById(R.id.TotalTextView);
    EnterPercentage = (EditText) findViewById(R.id.EnterPercentage);
    EnterPrice = (EditText) findViewById(R.id.EnterPrice);
    Button CalcBtn = (Button) findViewById(R.id.CalcBtn);


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

                float percentage = Float.parseFloat(EnterPercentage.getText().toString());
                float prix = Float.parseFloat(EnterPrice.getText().toString());
                float dec = percentage / 100;
                float total = prix - dec * Float.parseFloat(EnterPrice.getText().toString());

                TotalTextView.setText(String.format("%.2f", total));


        }
    });

Thank for your answer, I am a begineer so... Thanks !

if (EnterPercentage.getText().equals("")) {

                float percentage = Float.parseFloat(EnterPercentage.getText().toString());
                float prix = Float.parseFloat(EnterPrice.getText().toString());
                float dec = percentage / 100;
                float total = prix - dec * Float.parseFloat(EnterPrice.getText().toString());
                TotalTextView.setText(String.format("%.2f", total));


            } else {

}

the button don't do anything but the app doesn't crash


回答1:


We check the length of the String... when it's 0 then we do nothing. When the String > 0 your code is running.

Like:

            if (EnterPercentage.getText().trim().toString().length() == 0 || EnterPrice.getText().toString().length() == 0 ) {
                //Textfields are empty.
                Log.d("Error","Fields are empty");
            } else {
                //Textfield is full
                float percentage = Float.parseFloat(EnterPercentage.getText().toString());
                float prix = Float.parseFloat(EnterPrice.getText().toString());
                float dec = percentage / 100;
                float total = prix - dec * Float.parseFloat(EnterPrice.getText().toString());
                TotalTextView.setText(String.format("%.2f", total));
            }



回答2:


You need to check (obviously) your string. The if that your code is missing is :

if (TextUtils.isEmpty(EnterPercentage.getText()) || TextUtils.isEmpty(EnterPrice.getText())) {
// YOUR CODE
}

Good luck with android development. Questions was already posted




回答3:


You surely got a NullPointerException. That's because the text is null and you put a .toString() after.

Try this:

float percenteage = 0;
float prix = 0;

if (EnterPercentage.getText() != null)
    percenteage = Float.parseFloat(EnterPercentage.getText().toString());
if (EnterPrice.getText() != null)
    prix = Float.parseFloat(EnterPercentage.getText().toString());



回答4:


you need to validate before trying to convert to float

float percentage = getFloat();
private float getFloat(){
    float fRet = 0.0f;
    if(your validation here){
        fRet = Float.parseFloat(EnterPercentage.getText().toString());;
    }
    return fRet;
}


来源:https://stackoverflow.com/questions/40185478/how-to-stop-the-app-crashing-when-string-is-empty

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!