Question

I'm trying to send an email from my email class but when the program gets to startActivity it crashes I think it might has something to do with the manifest. Below is my main activity

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

public class InvoiceActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void SendMessage(View v)
    {
        // get email parameters
        SMTPmail mail = new SMTPmail();
        mail.SendSMTP("body of email","subject of email","recipient@example.com");
    }   
}

here is the SMTPmail

import android.app.Activity;
import android.content.Intent;
import android.widget.Toast;

public class SMTPmail extends Activity {

    public void SendSMTP(String message, String subject, String recipted)
    {
        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("message/rfc822");
        i.putExtra(Intent.EXTRA_EMAIL  , new String[]{recipted});
        i.putExtra(Intent.EXTRA_CC, "");
        i.putExtra(Intent.EXTRA_SUBJECT, subject);
        i.putExtra(Intent.EXTRA_TEXT   , message);
        try {
            startActivity(Intent.createChooser(i, "Send mail..."));//crashes here in debug
            finish();
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(SMTPmail.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
        }
    }
}

this is the manifest

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:label="@string/app_name"
        android:name=".InvoiceActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity 
        android:name=".SMTPmail" 
        android:noHistory="true">
    </activity>
</application>

logcat below

    01-07 00:01:25.199: D/dalvikvm(14198): GC_EXTERNAL_ALLOC freed 43K, 51% free 2687K/5379K, external 0K/0K, paused 54ms
    01-07 00:01:30.389: D/AndroidRuntime(14198): Shutting down VM
    01-07 00:01:30.389: W/dalvikvm(14198): threadid=1: thread exiting with uncaught exception (group=0x40018560)
    01-07 00:01:30.399: E/AndroidRuntime(14198): FATAL EXCEPTION: main
    01-07 00:01:30.399: E/AndroidRuntime(14198): java.lang.IllegalStateException: Could not execute method of the activity
    01-07 00:01:30.399: E/AndroidRuntime(14198):    at android.view.View$1.onClick(View.java:2165)
    01-07 00:01:30.399: E/AndroidRuntime(14198):    at android.view.View.performClick(View.java:2506)
    01-07 00:01:30.399: E/AndroidRuntime(14198):    at android.view.View$PerformClick.run(View.java:9112)
    01-07 00:01:30.399: E/AndroidRuntime(14198):    at android.os.Handler.handleCallback(Handler.java:587)
    01-07 00:01:30.399: E/AndroidRuntime(14198):    at android.os.Handler.dispatchMessage(Handler.java:92)
    01-07 00:01:30.399: E/AndroidRuntime(14198):    at android.os.Looper.loop(Looper.java:130)
    01-07 00:01:30.399: E/AndroidRuntime(14198):    at android.app.ActivityThread.main(ActivityThread.java:3835)
    01-07 00:01:30.399: E/AndroidRuntime(14198):    at java.lang.reflect.Method.invokeNative(Native Method)
    01-07 00:01:30.399: E/AndroidRuntime(14198):    at java.lang.reflect.Method.invoke(Method.java:507)
    01-07 00:01:30.399: E/AndroidRuntime(14198):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
    01-07 00:01:30.399: E/AndroidRuntime(14198):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
    01-07 00:01:30.399: E/AndroidRuntime(14198):    at dalvik.system.NativeStart.main(Native Method)
    01-07 00:01:30.399: E/AndroidRuntime(14198): Caused by: java.lang.reflect.InvocationTargetException
    01-07 00:01:30.399: E/AndroidRuntime(14198):    at java.lang.reflect.Method.invokeNative(Native Method)
    01-07 00:01:30.399: E/AndroidRuntime(14198):    at java.lang.reflect.Method.invoke(Method.java:507)
    01-07 00:01:30.399: E/AndroidRuntime(14198):    at android.view.View$1.onClick(View.java:2160)
    01-07 00:01:30.399: E/AndroidRuntime(14198):    ... 11 more
    01-07 00:01:30.399: E/AndroidRuntime(14198): Caused by: java.lang.NullPointerException
    01-07 00:01:30.399: E/AndroidRuntime(14198):    at android.app.Activity.startActivityForResult(Activity.java:2827)
    01-07 00:01:30.399: E/AndroidRuntime(14198):    at android.app.Activity.startActivity(Activity.java:2933)
    01-07 00:01:30.399: E/AndroidRuntime(14198):    at com.android.EUROPE.Invoice.SMTPmail.SendSMTP(SMTPmail.java:17)
    01-07 00:01:30.399: E/AndroidRuntime(14198):    at com.android.EUROPE.Invoice.InvoiceActivity.SendMessage(InvoiceActivity.java:19)
    01-07 00:01:30.399: E/AndroidRuntime(14198):    ... 14 more
Was it helpful?

Solution

As James already said, there is no reason for SMTPmail to extend Activity. A simple implementation could look like this.

SMTPmail:

public class SMTPmail {

    public static void sendSMTP(Context context, String message, String subject, String recipted)
    {
        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("message/rfc822");
        i.putExtra(Intent.EXTRA_EMAIL  , new String[]{recipted});
        i.putExtra(Intent.EXTRA_CC, "");
        i.putExtra(Intent.EXTRA_SUBJECT, subject);
        i.putExtra(Intent.EXTRA_TEXT   , message);

        try {
            context.startActivity(Intent.createChooser(i, "Send mail..."));//crashes here in debug
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(context, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
        }
    }
}

In InvoiceActivity:

public void SendMessage(View v)
{
    SMTPmail.sendSMTP(this, "body of email","subject of email","recipient@example.com");
}

OTHER TIPS

Is there a reason SMTPmail extends Activity? This looks superfluous. Just change STMPmail so that it does not extend any classes and this should work. You will have to pass a Context into that method to get things like startActivity().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top