Android 当我尝试发送电子邮件时,应用程序崩溃
当我按下一个按钮时,应用程序需要将所有信息传送到gmail或任何邮件应用程序,但它崩溃了。 这是我用过的Android 当我尝试发送电子邮件时,应用程序崩溃,android,Android,当我按下一个按钮时,应用程序需要将所有信息传送到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(Int
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 :"));
将startActivity()
包装在try/catch
中,以避免在没有活动处理您的意图的情况下发生崩溃(您应阅读堆栈跟踪(通过LogCat),您将在那里看到ActivityNotFoundException
)
试试这个代码。如果设备上没有能够处理intent的应用程序,在您的情况下,
intent.ACTION\u 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();
}
试试这个代码
下面是使用此代码和教程的示例
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;
}
}
祝你好运你能帮我发一下日志信息吗?
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;
}
}