Darko Petkovski
Darko Petkovski

Reputation: 3912

Android cannot find string if its null

I want to send Emails using android intent. Every thing is working well except when choosing email app to send email with, in the send to field getting null value, although I check for null values but seems I cannot detect when a string is null. Can anybody help me solve this.

if (emailAddress[0]!=null && !emailAddress[0].isEmpty()) {
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_EMAIL, emailAddress);
                intent.putExtra(Intent.EXTRA_SUBJECT,
                        getResources().getString(R.string.email_sub));
                // intent.putExtra(Intent.EXTRA_TEXT, "I'm email body.");

                startActivity(Intent.createChooser(intent, "Send Email"));

Upvotes: 3

Views: 2244

Answers (6)

itsrajesh4uguys
itsrajesh4uguys

Reputation: 4638

Check the String with equals() or equalsingorecase();

String[]  emailAddress = new String[10]; 

          emailAddress[0]="asdfasdfasdfasdf";

          if (emailAddress[0]!=null && !emailAddress[0].isEmpty())
          {
              System.err.println("asddddddd  " +emailAddress[0] );
          }

Upvotes: 2

akaya
akaya

Reputation: 1140

The string may not be null. AFAIK Intent.EXTRA_EMAIL takes String array as second argument.

Try this.

String[] addressArray = {"[email protected]", "[email protected]"};

//some lines...

intent.putExtra(Intent.EXTRA_EMAIL, addressArray);

Upvotes: 0

Ajay
Ajay

Reputation: 1189

it might help you..

you can use any from below 2

if (emailAddress[0].toString().equals(""))

or

if (emailAddress[0].toString().length() > 0)

Upvotes: 0

Bhargav Panchal
Bhargav Panchal

Reputation: 1179

TextUtils.isEmpty(charSequence char) is used for null String if it don't work then check if(emailAddress != null) because emailAddress[0] position value is null then your array is also null.

Upvotes: 0

amukhachov
amukhachov

Reputation: 5900

You can use TextUtils.isEmpty(CharSequence str) method to detect if String is empty or null

Upvotes: 0

steevoo
steevoo

Reputation: 631

try to use this syntax its detect null and empty string

if(!TextUtils.isEmpty(emailAddress[0]))

Upvotes: 2

Related Questions