0

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.

Moeez
  • 494
  • 9
  • 55
  • 147
  • You seem to dismiss a dialog in the SendEmailTask and since it is asynchronous this can lead to this error if that Activity is already finished for example. You may want to post that code here if you need more detailed help. – donfuxx Jan 27 '18 at 04:29
  • 2
    what's on line 302 in BackgroundMail.java ? – Rakibul Haq Jan 27 '18 at 04:30
  • your stacktrace says that you call dialog dismiss here: `android.app.Dialog.dismiss(Dialog.java:365) at com.creativityapps.gmailbackgroundlibrary.BackgroundMail$SendEmailTask.onPostExecute(BackgroundMail.java:302)` that is the interesting code that you should include in the question – donfuxx Jan 27 '18 at 04:37
  • @R.Haq I have added the piece of cod for `302` and `265` error, also I can't edit it as it is locked. – Moeez Jan 27 '18 at 05:22
  • @donfuxx I have updated my question can you please see it ? – Moeez Jan 27 '18 at 05:24
  • i think this thread may be of a great help to you: https://stackoverflow.com/questions/2745061/java-lang-illegalargumentexception-view-not-attached-to-window-manager – Rakibul Haq Jan 27 '18 at 05:44
  • @R.Haq I have already seen this, but the problem is I can't edit it as the file is locked :( – Moeez Jan 27 '18 at 05:52

1 Answers1

1

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.

donfuxx
  • 11,277
  • 6
  • 44
  • 76
  • I can't edit any piece of code as the file is locked :( – Moeez Jan 27 '18 at 05:47
  • That is unfortunate.. I suggest you find the developer who locked the file to unlock it for you. – donfuxx Jan 27 '18 at 05:58
  • I am using the external library, downloaded from `compile 'com.github.yesidlazaro:GmailBackground:1.2.0'` – Moeez Jan 27 '18 at 06:00
  • 1
    Contact the developer of that library and report the issue or switch to another library or develop your own email sender. Or last not least since it is an open source project from github: fork it and fix it ;-) – donfuxx Jan 27 '18 at 06:02
  • Where have you find it? as this package was also on the github – Moeez Jan 27 '18 at 07:01
  • So you are saying I should create separate files (same like in the library) and then add them into my project ? – Moeez Jan 27 '18 at 07:30
  • It is up to you. Either contact third party developers and hope they fix it or do it yourself. – donfuxx Jan 27 '18 at 07:37