0

我正在尝试从我的电子邮件类发送一封电子邮件,但是当程序启动时它崩溃了,我认为它可能与清单有关。以下是我的主要活动

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");
    }   
}

这是 SMTP 邮件

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();
        }
    }
}

这是清单

<?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>

下面的日志猫

    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
4

2 回答 2

2

正如詹姆斯已经说过的,没有理由SMTPmail延长Activity. 一个简单的实现可能如下所示。

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();
        }
    }
}

InvoiceActivity

public void SendMessage(View v)
{
    SMTPmail.sendSMTP(this, "body of email","subject of email","recipient@example.com");
}
于 2013-01-07T00:18:10.170 回答
1

有没有理由SMTPmail延长Activity?这看起来是多余的。只需更改 STMPmail 使其不扩展任何类,这应该可以工作。您必须将 a 传递给Context该方法才能获得startActivity().

于 2013-01-07T00:08:04.650 回答