繁体   English   中英

当我尝试发送电子邮件时,应用崩溃

[英]When i try to send email the app crashes

当我按下按钮时,应用程序需要将信息发送到gmail或任何邮件应用程序,但它会崩溃。 这就是我用的。

Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{"youremail@yahoo.com"});          
email.putExtra(Intent.EXTRA_SUBJECT, "subject");
email.putExtra(Intent.EXTRA_TEXT, "message");
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Choose an Email client :"));

试试这个代码。 如果设备上没有可以处理该意图的应用程序(在您的情况下为Intent.ACTION_SEND ,则该应用程序将崩溃。

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"recipient@example.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT   , "body of email");
try {
    startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(MyActivity.this, "There are no email clients installed.",Toast.LENGTH_SHORT).show();
}

startActivity()包装在try/catch以避免在没有活动可处理您的意图的情况下发生崩溃(您应该(通过LogCat读取堆栈跟踪,并且在那里会看到ActivityNotFoundException ))

Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{"youremail@yahoo.com"});          
email.putExtra(Intent.EXTRA_SUBJECT, "subject");
email.putExtra(Intent.EXTRA_TEXT, "message");
email.setType("message/rfc822");

try {
    startActivity(Intent.createChooser(email, "Choose an Email client :"));
} catch (Exception e) {
     e.printStackTrace();

     // show toast etc to the user
}

试试这个代码

这里是带有此代码的存储库 ,还有这里的教程

private EditText to;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    to= (EditText)findViewById(R.id.to);
}   


public void Enviar(View view) {

    Intent email = new Intent(Intent.ACTION_SEND);
    email.putExtra(Intent.EXTRA_EMAIL, new String[]{to.getText().toString()});
    email.putExtra(Intent.EXTRA_CC, new String[]{ to});
    email.putExtra(Intent.EXTRA_BCC, new String[]{to});
    email.putExtra(Intent.EXTRA_SUBJECT, asunto.getText().toString());
    email.putExtra(Intent.EXTRA_TEXT, cuerpo.getText().toString());

    //need this to prompts email client only
    email.setType("message/rfc822");

    startActivity(Intent.createChooser(email, "Choose an Email client :"));

}}


 //method get account the device
static String getEmail(Context context) {
AccountManager accountManager = AccountManager.get(context);
Account[] accounts = accountManager.getAccounts();
Account account = accounts[0];//getAccount(accountManager);

if (account == null) {
    return null;
} else {
    return account.name;
}
}

在此处输入图片说明

祝好运

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM