crash in Visual Studio when attempting to send an email from Android

motomoto CAMember ✭✭

My application is crashing when attempting to send an email from Android. Here's my code:

    public class EmailHelper : Activity, IEmail
    {
    ...
        private bool CreateEmail(string attachment)
        {
            var email = new Intent(Intent.ActionSend);
            SettingsDataAdapter mySettingsDataAdapter = App.MySettingsDataAdapter;
            email.PutExtra(Android.Content.Intent.ExtraEmail, new string[] { "[email protected]"});
            email.PutExtra(Android.Content.Intent.ExtraSubject, "test email");
            email.PutExtra(Android.Content.Intent.ExtraText, "see attachment");
            email.SetType("message/rfc822");
            var file = new Java.IO.File(attachment);
            file.SetReadable(true, false);
            var uri = Android.Net.Uri.FromFile(file);
            email.PutExtra(Intent.ExtraStream, uri);
            email.SetFlags(ActivityFlags.NewTask);
            StartActivity(email);
            return true;
        }
    ...
    }

and the first error in the output window is:
attempt to invoke virtual method 'android.app.activitythread$applicationthread android.app.activitythread.getapplicationthread()' on a null object reference

Any ideas? Thanks in advance!

Best Answer

  • motomoto CA ✭✭
    Accepted Answer

    Found out after more digging that an Activity cannot be started without a context. I ended up changing

    StartActivity(email);
    

    with

    ((Activity)Forms.Context).StartActivity(email);
    

    and that specific error went away.

    Just a side note that the next error I hit was "no provider found", which was due to not having an email app configured in the emulator.

Answers

  • AnbuMani27AnbuMani27 USMember ✭✭✭
    edited July 2017

    Hi @moto
    Try This

     Intent emailIntent = new Intent(Android.Content.Intent.ActionSend);
                        emailIntent.SetType("plain/text");
                        emailIntent.PutExtra(Android.Content.Intent.ExtraSubject, "REG ");
                        emailIntent.PutExtra(Android.Content.Intent.ExtraText, "TEXT");
                        StartActivity(emailIntent);
    
  • motomoto CAMember ✭✭

    @AnbuVijay27: unfortunately no luck... I get the exact same error when running your code suggestion, verbatim. Thanks anyway.

  • motomoto CAMember ✭✭
    Accepted Answer

    Found out after more digging that an Activity cannot be started without a context. I ended up changing

    StartActivity(email);
    

    with

    ((Activity)Forms.Context).StartActivity(email);
    

    and that specific error went away.

    Just a side note that the next error I hit was "no provider found", which was due to not having an email app configured in the emulator.

Sign In or Register to comment.