Java 在新实例中发送电子邮件活动

Java 在新实例中发送电子邮件活动,java,android,android-intent,start-activity,Java,Android,Android Intent,Start Activity,我正试图从我的email类发送一封电子邮件,但当程序进入startActivity时,它崩溃了,我想这可能与清单有关。以下是我的主要活动 import android.app.Activity; import android.os.Bundle; import android.view.View; public class InvoiceActivity extends Activity { /** Called when the activity is first created.

我正试图从我的email类发送一封电子邮件,但当程序进入startActivity时,它崩溃了,我想这可能与清单有关。以下是我的主要活动

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");
    }   
}
这是短信

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

是否有原因
SMTPmail
扩展
活动
?这看起来是多余的。只需更改STMPmail,使其不扩展任何类,这应该可以工作。您必须将
上下文
传递到该方法中,才能获得类似
startActivity()

的内容是否有原因
SMTPmail
扩展
活动
?这看起来是多余的。只需更改STMPmail,使其不扩展任何类,这应该可以工作。您必须将
上下文
传递到该方法中,才能获得类似
startActivity()

的内容,正如James已经说过的那样,
SMTPmail
没有理由扩展
活动
。一个简单的实现可能是这样的

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();
        }
    }
}
发票活动中

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

正如James已经说过的那样,
SMTPmail
没有理由扩展
活动。一个简单的实现可能是这样的

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();
        }
    }
}
发票活动中

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

我们需要一个完整的LogCat输出来帮助您更好地工作。跳出的一件事是,您正在通过其构造函数实例化一个活动。永远不要这样做。@A--C很抱歉听起来像个傻瓜,但应该怎么做呢?内贾姆斯告诉我,这只是一个小的重构。我们需要一个完整的LogCat输出来帮助您更好地工作。跳出的一件事是,您正在通过其构造函数实例化一个活动。永远不要这样做。@A--C很抱歉听起来像个傻瓜,但应该怎么做呢?内贾姆斯击败了我,这只是一个小的重构。我加入了这个活动来让startActivity工作。目前正在删除活动,尝试了解如何获得startActivity,并完成与上下文相关的活动,以使startActivity正常工作。目前正在删除活动,尝试了解如何获得startActity并完成与contextyea的工作我无法在开始时使其保持静态,因为我有扩展活动go figureyea我无法在开始时使其保持静态,因为我有扩展活动go figureyea