我使用以下代码发送电子邮件
public class Mail extends Activity{
public void send(String mail, String subject) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{mail});
intent.putExtra(Intent.EXTRA_TEXT, "Some text");
startActivity(intent);
}
}
但我在NullPointerException
startActivity(intent)...
我的权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
我不知道是什么原因。有人能帮助我吗?
06-12 18:28:22.840: E/AndroidRuntime(11859): FATAL EXCEPTION: main
06-12 18:28:22.840: E/AndroidRuntime(11859): java.lang.NullPointerException
06-12 18:28:22.840: E/AndroidRuntime(11859): at android.content.ContextWrapper.getPackageManager(ContextWrapper.java:86)
06-12 18:28:22.840: E/AndroidRuntime(11859): at puma.export_and_send.Mail.send(Mail.java:20)
06-12 18:28:22.840: E/AndroidRuntime(11859): at puma.dialogs.JourneyDialog$8.onClick(JourneyDialog.java:175)
06-12 18:28:22.840: E/AndroidRuntime(11859): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
06-12 18:28:22.840: E/AndroidRuntime(11859): at android.os.Handler.dispatchMessage(Handler.java:99)
06-12 18:28:22.840: E/AndroidRuntime(11859): at android.os.Looper.loop(Looper.java:137)
06-12 18:28:22.840: E/AndroidRuntime(11859): at android.app.ActivityThread.main(ActivityThread.java:4802)
06-12 18:28:22.840: E/AndroidRuntime(11859): at java.lang.reflect.Method.invokeNative(Native Method)
06-12 18:28:22.840: E/AndroidRuntime(11859): at java.lang.reflect.Method.invoke(Method.java:511)
06-12 18:28:22.840: E/AndroidRuntime(11859): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:813)
06-12 18:28:22.840: E/AndroidRuntime(11859): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:580)
06-12 18:28:22.840: E/AndroidRuntime(11859): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
试
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "test@test.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, "");
startActivity(Intent.createChooser(emailIntent, "Email"));
答案 1 :(得分:0)
检查变量subject
和mail
的值,其中一个必须具有null
值!
public void send(String mail, String subject) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{mail});
intent.putExtra(Intent.EXTRA_TEXT, "Some text");
startActivity(intent);
}
要避免这种情况,您将添加验证并在Toast中显示消息:
public void send(String mail, String subject) {
if(mail!=null && subject != null){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{mail});
intent.putExtra(Intent.EXTRA_TEXT, "Some text");
startActivity(intent);
}else{
Toast.makeText(getApplicationContext(), "Cannot execute, one of the values \"mail\" or \"subject\" is NULL!" , Toast.LENGTH_LONG).show();
}
}
答案 2 :(得分:0)
检查mail
,subject
不是null
public void send(String mail, String subject) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{mail});
intent.putExtra(Intent.EXTRA_TEXT, "Some text");
// Verify that the intent will resolve to an activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
的最佳方式
答案 3 :(得分:0)
Might be because the launcher doesn't recognize the e-mail app, Try by using Chooser
intent = new Intent(Intent.ACTION_SEND);
intent.setData(Uri.parse("mailto:"));
String[] to ={"XX","XX "};
intent.putExtra(Intent.EXTRA_EMAIL,to);
intent.putExtra(Intent.EXTRA_SUBJECT,"XX");
intent.putExtra(Intent.EXTRA_TEXT,"This E-mail has been sent by the Customer through XX");
intent.setType("message/rfc822");
chooser = Intent.createChooser(intent,"Send Email");
startActivity(chooser);