简体   繁体   中英

Checking if EditText is empty crashes application

i am trying to create a simple android app using eclipse and android sdk (java) i have a EditText box with a few restrictions but it crashes when the EditText box is empty i have tried so many ways on checking the EditText if its empty but it just does want to work.. my code is below why does it always crash when the box is empty it should be simple no?

buttonHash.setOnClickListener(new View.OnClickListener(){

    public void onClick(View v){
        switch(v.getId()){
        case R.id.hash_button:
        TextView msg = (TextView)findViewById(R.id.tell);
        info = (EditText)findViewById(R.id.entry);
        anss = info.getText().toString();
        //String ans = Double.toString(res);
        double result = Double.parseDouble(anss);
        if (res == result){
        msg.setText("Correct");
        }else
        if (res != result){
            msg.setText("Incorrect");
            }else
        if (info.getText().toString().equals("")){
            msg.setText("Empty!");
            }
        }       
    }
});

First check for info.getText() is null

if (info.getText() == null || "".equals(info.getText().toString())){ 
            msg.setText("Empty!"); 
            }

Please note exception might be here, and exception is NumberFormatException, coz you are trying to parse double from "".

anss = info.getText().toString();
        //String ans = Double.toString(res);
        double result = Double.parseDouble(anss);

so do the following to avoid this issue:

double result = 0;

try{

    result = Double.parseDouble(anss);

}catch(NumberFormatException ex)
{
}

You can use this code to solve your problem.

    public void onClick(View v) {
            if (_text.getText().toString().equals(""))
                Toast.makeText(getApplicationContext(), "Empty BOX", 5000).show();
            else
                Toast.makeText(getApplicationContext(), _text.getText().toString(), 5000).show();

        }

Thats it.. Try this ...it will works.

try this i have just added a alert window

public void onClick(View v) { if (_text.getText().toString().equals("")){

                   AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Text is empty");
            builder.setCancelable(true);
            builder.setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            // TODO Auto-generated method stub
                            dialog.cancel();
                        }
                    });
            builder.create().show();

            }

            else
                Toast.makeText(getApplicationContext(), _text.getText().toString(), 5000).show();

        }

EditText value will never be null..

Use it in following way:

if ((info.getText().toString().trim().getLength() == 0) && ("").equals(info.getText().toString().trim())){
            msg.setText("Empty!");
            }
        } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM