我正在开发一个应用程序,它使用片段内的JavaMail API发送电子邮件。整个应用程序工作正常,但当我点击按钮发送电子邮件时,应用程序崩溃。我试图改变SendMail代码中的上下文,但它没有解决我的问题。我发布了调试错误,我正在获取和我的Java代码,如果你还需要其他任何东西,请问我。任何帮助都非常受欢迎。
SendMail.Java
public class SendMail extends AsyncTask<Void,Void,Void> {
//Declaring Variables
private Context context;
private Session session;
//Information to send email
private String email;
private String subject;
private String message;
//Progressdialog to show while sending email
private ProgressDialog progressDialog;
//Class Constructor
public SendMail(OneFragment oneFragment, String email, String subject, String message) {
//Initializing variables
this.context = context;
this.email = email;
this.subject = subject;
this.message = message;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
//Showing progress dialog while sending email
progressDialog = ProgressDialog.show(context,"Enviando mensagem","Aguarde um momento...",false,false);
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
//Dismissing the progress dialog
progressDialog.dismiss();
//Showing a success message
Toast.makeText(context,"Mensagem enviada",Toast.LENGTH_LONG).show();
}
@Override
protected Void doInBackground(Void... params) {
//Creating properties
Properties props = new Properties();
//Configuring properties for gmail
//If you are not using gmail you may need to change the values
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
//Creating a new session
session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
//Authenticating the password
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(Config.EMAIL,Config.PASSWORD);
}
});
try {
//Creating MimeMessage object
MimeMessage mm = new MimeMessage(session);
//Setting sender address
mm.setFrom(new InternetAddress(Config.EMAIL));
//Adding receiver
mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
//Adding subject
mm.setSubject(subject);
//Adding message
mm.setText(message);
//Sending email
Transport.send(mm);
} catch (MessagingException e) {
e.printStackTrace();
}
return null;
}
}
错误调试
E/AndroidRuntime: FATAL EXCEPTION: main
Process: info.androidhive.materialtabs, PID: 6870
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:154)
at android.app.AlertDialog.<init>(AlertDialog.java:109)
at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
at android.app.ProgressDialog.show(ProgressDialog.java:110)
at android.app.ProgressDialog.show(ProgressDialog.java:104)
at info.androidhive.materialtabs.SendMail.onPreExecute(SendMail.java:50)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
at android.os.AsyncTask.execute(AsyncTask.java:535)
at info.androidhive.materialtabs.fragments.OneFragment.sendEmail(OneFragment.java:45)
at info.androidhive.materialtabs.fragments.OneFragment.onClick(OneFragment.java:50)
at android.view.View.performClick(View.java:5156)
at android.view.View$PerformClick.run(View.java:20755)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
OneFragment
//More Code
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);}
private void sendEmail() {
//Getting content for email
String email = "myemail";
String subject = "subject";
String message = editTextMessage.getText().toString().trim();
//Creating SendMail object
SendMail sm = new SendMail(this, email, subject, message);
//Executing sendmail to send email
sm.execute();
}
@Override
public void onClick(View v) {
sendEmail();
}
//More Code
答案 0 :(得分:1)
检查SendMail任务的构造函数
//Class Constructor
public SendMail(OneFragment oneFragment, String email, String subject, String message) {
//Initializing variables
this.context = context;
this.email = email;
this.subject = subject;
this.message = message;
}
您没有收到context
值,因此context
为空:)
然后你有问题:
progressDialog = ProgressDialog.show(context,"Enviando mensagem","Aguarde um momento...",false,false);
更新:修复您的代码
//Class Constructor
public SendMail(OneFragment oneFragment, String email, String subject, String message) {
//Initializing variables
this.context = oneFragment.getActivity();
this.email = email;
this.subject = subject;
this.message = message;
}