Java – Android- java.lang.IllegalArgumentException

androidexceptionjava

I already have seen this question. But couldn't figure out what's the issue.
I am sending an email in background using BackgroundMail in my ImageSyncReciever class. But when email is sent my app crashes while giving me the below error

FATAL EXCEPTION: main
Process: com.thumbsol.accuratemobileassetsmanagament, PID: 7480
java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{300e55de V.E….. R…..I. 0,0-0,0} not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:434)
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:353)
at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:116)
at android.app.Dialog.dismissDialog(Dialog.java:382)
at android.app.Dialog.dismiss(Dialog.java:365)
at com.creativityapps.gmailbackgroundlibrary.BackgroundMail$SendEmailTask.onPostExecute(BackgroundMail.java:302)
at com.creativityapps.gmailbackgroundlibrary.BackgroundMail$SendEmailTask.onPostExecute(BackgroundMail.java:265)
at android.os.AsyncTask.finish(AsyncTask.java:636)
at android.os.AsyncTask.access$500(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5660)
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:963)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)

Below is my code in which I am sending the email

     if (response.body().getStatus().equals("OK")) {

                            snapManager.updateSnapStatus(AssetsManagementContract.SnapEntry.COLUMN_SITE_SNAP, snap.getSnapName(), Constants.SNAP_SYNCED);
                            Intent broadcastSyc = new Intent();
                            broadcastSyc.setAction(Common.GetSyncImageAction());
                            broadcastSyc.putExtra("STATUS", true);
                            mContext.sendBroadcast(broadcastSyc);
                            sendImage(mContext);
                            BackgroundMail.newBuilder(mContext)
                                    .withUsername("gmail id")
                                    .withPassword("pass")
                                    .withMailto("gmail id")
                                    .withType(BackgroundMail.TYPE_PLAIN)
                                    .withSubject("New Meter Installation")
                                    .withBody("Meter #" + msn + " is "+ com+ " and "+ status)
                                    .send();

                        }

How can i resolve this issue? Any help would be highly appreciated

Note: The email is sent when the form is submitted and after saving I am not using any dialog.

Update 1
Below is the BackgroudMailer class function

public class SendEmailTask extends AsyncTask<String, Void, Boolean> {
    private ProgressDialog progressDialog;

    public SendEmailTask() { //error onPostExecute(BackgroundMail.java:265)
    }

    protected void onPreExecute() {
        super.onPreExecute();
        if(BackgroundMail.this.processVisibility) {
            this.progressDialog = new ProgressDialog(BackgroundMail.this.mContext);
            this.progressDialog.setMessage(BackgroundMail.this.sendingMessage);
            this.progressDialog.setCancelable(false);
            this.progressDialog.show();
        }

    }

    protected Boolean doInBackground(String... arg0) {
        try {
            GmailSender sender = new GmailSender(BackgroundMail.this.username, BackgroundMail.this.password);
            if(!BackgroundMail.this.attachments.isEmpty()) {
                for(int i = 0; i < BackgroundMail.this.attachments.size(); ++i) {
                    if(!((String)BackgroundMail.this.attachments.get(i)).isEmpty()) {
                        sender.addAttachment((String)BackgroundMail.this.attachments.get(i));
                    }
                }
            }

            sender.sendMail(BackgroundMail.this.subject, BackgroundMail.this.body, BackgroundMail.this.username, BackgroundMail.this.mailto, BackgroundMail.this.type);
        } catch (Exception var4) {
            var4.printStackTrace();
            return Boolean.valueOf(false);
        }

        return Boolean.valueOf(true);
    }

    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        if(BackgroundMail.this.processVisibility) {
            this.progressDialog.dismiss(); // error onPostExecute(BackgroundMail.java:302)
            if(result.booleanValue()) {
                if(!TextUtils.isEmpty(BackgroundMail.this.sendingMessageSuccess)) {
                    Toast.makeText(BackgroundMail.this.mContext, BackgroundMail.this.sendingMessageSuccess, 0).show();
                }

                if(BackgroundMail.this.onSuccessCallback != null) {
                    BackgroundMail.this.onSuccessCallback.onSuccess();
                }
            } else {
                if(!TextUtils.isEmpty(BackgroundMail.this.sendingMessageError)) {
                    Toast.makeText(BackgroundMail.this.mContext, BackgroundMail.this.sendingMessageError, 0).show();
                }

                if(BackgroundMail.this.onFailCallback != null) {
                    BackgroundMail.this.onFailCallback.onFail();
                }
            }
        }

    }
}

The problem is I cannot edit it as the file is locked.

Best Answer

in onPostExecute you dismiss the dialog without checking if it is actually shown:

this.progressDialog.dismiss();

add a check of isShowing for that (and a null-check just in case..)

if (progressDialog != null && progressDialog.isShowing()) { 
   progressDialog.dismiss();
}

Also I see that you use static references to contexts. That can lead to memory leaks, but that is just a side note.