Goran
Goran

Reputation: 1239

App crashes with click on button with empty editText

btnNadoplata.setOnClickListener(new View.OnClickListener()

        {
            public void onClick(View v) 
            {   
                long inputValue1 = Long.parseLong(text1.getText().toString());
                String encodedHash = Uri.encode("#");

                if (text1.getText().length() == 14 ) {

                    startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:*123*" + inputValue1 + encodedHash)));
                }else {

                    Toast.makeText(bonbon.this, "Krivi kod. Unesi 14-znamenkasti kod sa bona!", Toast.LENGTH_SHORT).show();

                    }
            }
        });

I have one editText, in wich user needs to input a number14 digit number. If number is less or more than an 14 digits, when user clikc on button, he gets the message that say input is not good. The problem is when editText is empty, and user click on button, app crashes. how can i change this, so if editText is empty, user gets message from above code part ??

Sory for my bad english.

Upvotes: 0

Views: 4181

Answers (3)

Parth Pandya
Parth Pandya

Reputation: 1490

objText = (EditText)findViewById(R.id.txtPurchasePrice);
String strPurchasePrice = objText.getText().toString();   
objText = (EditText)findViewById(R.id.txtSalePrice);
String strSalePrice = objText.getText().toString();
objText = (EditText)findViewById(R.id.txtShares);
String strShares = objText.getText().toString();

if(strPurchasePrice.trim().equals("") && strSalePrice.trim().equals("") && strShares.trim().equals("")) {      
   Toast.makeText(getApplicationContext(), "Please insert the Values..", 10000).show();
}

Upvotes: 0

Snicolas
Snicolas

Reputation: 38168

You should test your input length before parsing. Parsing crashes.

 public void onClick(View v) 
 { 
     if( text1.getText().length() <14 )
     {
        Toast.makeText(bonbon.this, "Krivi kod. Unesi 14-znamenkasti kod sa bona!", Toast.LENGTH_SHORT).show();
        return;
     }//if

     long inputValue1 = Long.parseLong(text1.getText().toString());
     String encodedHash = Uri.encode("#");
     startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:*123*" + inputValue1 + encodedHash)));
 }//met

An alternative could be to surround parsing with a try/catch/block, but it's less efficient than this simple test, but more robust if user types non digits.

Regards, Stéphane

Upvotes: 1

Cedekasme
Cedekasme

Reputation: 2037

It might crash on this line:

long inputValue1 = Long.parseLong(text1.getText().toString());

In fact, if you have an empty string in your EditText text1, the function parseLong() will throw a NumberFormatException exception.

You should test the value of the text of text1 before continuing:

public void onClick(View v) 
{   
    if (text1.getText().toString().compareTo("") == 0)
    {
        long inputValue1 = Long.parseLong(text1.getText().toString());
        String encodedHash = Uri.encode("#");
        ...

Or you can add try/catch instruction to catch the exception thrown by Long.parseLong().

public void onClick(View v) 
{   
    try
    {
        long inputValue1 = Long.parseLong(text1.getText().toString());
        String encodedHash = Uri.encode("#");
        ...
    }
    catch (NumberFormatException nfe)
    {
        ...
    }

Upvotes: 1

Related Questions