1

I use following code to send email

public class Mail extends Activity{

    public void send(String mail, String subject) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        intent.putExtra(Intent.EXTRA_EMAIL, new String[]{mail});
        intent.putExtra(Intent.EXTRA_TEXT, "Some text");
        startActivity(intent);  
    }
}

but I get NullPointerException in startActivity(intent)...

My permissions:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />

I have no idea what is the reason. Could someone help me?

   06-12 18:28:22.840: E/AndroidRuntime(11859): FATAL EXCEPTION: main

    06-12 18:28:22.840: E/AndroidRuntime(11859): java.lang.NullPointerException

    06-12 18:28:22.840: E/AndroidRuntime(11859):    at android.content.ContextWrapper.getPackageManager(ContextWrapper.java:86)

    06-12 18:28:22.840: E/AndroidRuntime(11859):    at puma.export_and_send.Mail.send(Mail.java:20)

    06-12 18:28:22.840: E/AndroidRuntime(11859):    at puma.dialogs.JourneyDialog$8.onClick(JourneyDialog.java:175)

    06-12 18:28:22.840: E/AndroidRuntime(11859):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)  

    06-12 18:28:22.840: E/AndroidRuntime(11859):    at android.os.Handler.dispatchMessage(Handler.java:99)

    06-12 18:28:22.840: E/AndroidRuntime(11859):    at android.os.Looper.loop(Looper.java:137)

    06-12 18:28:22.840: E/AndroidRuntime(11859):    at android.app.ActivityThread.main(ActivityThread.java:4802)

    06-12 18:28:22.840: E/AndroidRuntime(11859):    at java.lang.reflect.Method.invokeNative(Native Method)

    06-12 18:28:22.840: E/AndroidRuntime(11859):    at java.lang.reflect.Method.invoke(Method.java:511)

    06-12 18:28:22.840: E/AndroidRuntime(11859):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:813)

    06-12 18:28:22.840: E/AndroidRuntime(11859):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:580)

    06-12 18:28:22.840: E/AndroidRuntime(11859):    at dalvik.system.NativeStart.main(Native Method)
beresfordt
  • 5,088
  • 10
  • 35
  • 43
adolzi
  • 671
  • 2
  • 7
  • 15
  • Where do you get the `NullPointerException`? Stack trace pls. – Adam Arold Jun 12 '15 at 15:57
  • 1
    Use LogCat to examine the Java stack trace: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this Also, there are no `READ_INTERNAL_STORAGE` or `WRITE_INTERNAL_STORAGE` permissions in Android. – CommonsWare Jun 12 '15 at 15:57
  • messages displayed in a LogCat must be a requirement in a Question here @ StackOverflow! – Jorgesys Jun 12 '15 at 16:02
  • where are you running it ? Real device or emulator ? – Blackbelt Jun 12 '15 at 16:17
  • I'm running it on my phone – adolzi Jun 12 '15 at 16:22
  • @adolzi inside the messages displayed in LogCat, you will find a line "Caused by...." that will describe your problem, the messages that you have posted doesn´t have any clue to find the problem.... You will add the validation described in my answer :) – Jorgesys Jun 12 '15 at 17:34
  • But I copied all what I got.. – adolzi Jun 12 '15 at 17:38

4 Answers4

0

try

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "test@test.com", null));
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
        emailIntent.putExtra(Intent.EXTRA_TEXT, "");
        startActivity(Intent.createChooser(emailIntent, "Email"));
Galax
  • 367
  • 1
  • 13
0

check the value of the variables subject and mail one of them must have a null value!

public void send(String mail, String subject) {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    intent.putExtra(Intent.EXTRA_EMAIL, new String[]{mail});
    intent.putExtra(Intent.EXTRA_TEXT, "Some text");
    startActivity(intent);  
}

to avoid this you will add a validation and a message displayed in a Toast:

public void send(String mail, String subject) {
    if(mail!=null && subject != null){
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        intent.putExtra(Intent.EXTRA_EMAIL, new String[]{mail});
        intent.putExtra(Intent.EXTRA_TEXT, "Some text");
        startActivity(intent);  
    }else{
        Toast.makeText(getApplicationContext(), "Cannot execute, one of the values \"mail\" or \"subject\" is NULL!" , Toast.LENGTH_LONG).show();             
    }
}
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
0

Check mail and subject is not null

public void send(String mail, String subject) {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    intent.putExtra(Intent.EXTRA_EMAIL, new String[]{mail});
    intent.putExtra(Intent.EXTRA_TEXT, "Some text");

    // Verify that the intent will resolve to an activity
    if (sendIntent.resolveActivity(getPackageManager()) != null) {
       startActivity(intent);  
    }

}

best way to send implicit Intent

N J
  • 27,217
  • 13
  • 76
  • 96
0
Might be because the launcher doesn't recognize the e-mail app, Try by using Chooser

  intent = new Intent(Intent.ACTION_SEND);
                intent.setData(Uri.parse("mailto:"));
                String[] to ={"XX","XX "};
                intent.putExtra(Intent.EXTRA_EMAIL,to);
                intent.putExtra(Intent.EXTRA_SUBJECT,"XX");
                intent.putExtra(Intent.EXTRA_TEXT,"This E-mail has been sent by the Customer through XX");
                intent.setType("message/rfc822");
                chooser = Intent.createChooser(intent,"Send Email");
                startActivity(chooser);