When I register a user in the app, an AsyncTask is executed that sends an email to my gmail account, this works correctly but generates an error and causes the new Activity to be loaded once and then loads the new same Activity.
This is the error in the Logcat
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.matia.tonum, PID: 9873
java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{30bd839 V.E..... R......D 0,0-456,242} not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:386)
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:312)
at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:84)
at android.app.Dialog.dismissDialog(Dialog.java:341)
at android.app.Dialog.dismiss(Dialog.java:324)
at com.example.matia.tonum.SendMail.onPostExecute(SendMail.java:53)
at com.example.matia.tonum.SendMail.onPostExecute(SendMail.java:19)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5258)
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:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
Line 53 is the progressDialog.dismiss (); in the following code
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
//Dismissing the progress dialog
progressDialog.dismiss();
//Showing a success message
Toast.makeText(context,"Empresa registrada",Toast.LENGTH_LONG).show();
}
and line 19 is (public class SendMail extends AsyncTask)
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(Context context, 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,"Registrando ","Espera, por favor...",false,false);
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
//Dismissing the progress dialog
progressDialog.dismiss();
//Showing a success message
Toast.makeText(context,"Empresa registrada",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;
}
}
And so I call AsyncTask to send the email in my Activity
private void sendEmail() {
//Getting content for email
String email = inputEmail.getText().toString().trim();
String subject = "Empresa " + nombreusuario + ", " + Ciudad + ", " + País + ".";
String message = "Datos de la empresa " + nombreusuario
+ '\n' + '\n' +
"Nombre: " + nombreusuario + '\n' +
"Dirección: " + direccion + '\n' +
"País: " + País + '\n' +
"Ciudad: " + Ciudad + '\n' +
"Tipo de Empresa: " + TipodeEmpresa + '\n' +
"Rut o DNI: " + rutodni + '\n' +
"Correo electrónico: " + email + '\n' +
"Contraseña: " + password + '\n' +
"ID Usuario: " + user_id + '\n' + '\n' +
"Activo: " + activo;
//Creating SendMail object
//SendMail sm = new SendMail(this, email, subject, message);
SendMail sendem = new SendMail(this, email, subject, message);
//Executing sendmail to send email
sendem.execute();
}
This AsyncTask I call it in the Activity where I register the users and after registering the user there is an Intent that takes us to the new Activity.
I think the error is because the app changes the Activity before I finish sending the email or finish running the AsyncTask, but I do not know how I could make it finish running first and then change the Activity ...
I hope you understand and can help me, thank you very much!