在旋转的AsyncTask中出现IllegalArgumentException

时间:2015-12-11 19:08:00

标签: java android android-asynctask

我已在后台成功发送了一封电子邮件,并在发送所述电子邮件时向用户显示了一个进度对话框。但是当用户在对话框启动时翻转屏幕时,我得到一个IllegalArgumentException。我已经尝试使用WeakReference对象,它似乎没有解决问题。这是我的AsyncTask类。

private class SendMailTask extends AsyncTask<Mail, String, EmailStatusResponce> {

    private final WeakReference<ProgressDialog> weakReference;


    public SendMailTask() {
        ProgressDialog progressDialog = new ProgressDialog(SendReportActivity.this);
        progressDialog.setMessage("Sending...");
        progressDialog.setCancelable(false);
        progressDialog.show();
        weakReference = new WeakReference<>(progressDialog);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected EmailStatusResponce doInBackground(Mail... mail) {
        return mail[0].send();
    }

    @Override
    protected void onPostExecute(EmailStatusResponce result) {
        super.onPostExecute(result);

        if (result != null && weakReference != null) {
            weakReference.get().dismiss();     // This is where the exception is thrown.

            if (result.isSuccess()) {
                Intent intent = new Intent(SendReportActivity.this, MainActivity.class);
                startActivity(intent);
            }
            else  {}
        }
    }

这是例外

  

java.lang.IllegalArgumentException:View = com.android.internal.policy.PhoneWindow $ DecorView {688d3e2 VE ..... R ...... D 0,0-1026,348}未附加到窗口管理器

提前感谢您提供任何帮助。

编辑:更多logcat

  

---------崩溃的开始   12-11 16:22:40.154 1976-1976 / com.blazapps.allenfamilymedicine E / AndroidRuntime:FATAL EXCEPTION:main       过程:com.blazapps.allenfamilymedicine,PID:1976       java.lang.IllegalArgumentException:View = com.android.internal.policy.PhoneWindow $ DecorView {f89e667 V.E ...... R ...... D 0,0-760,232}未附加到窗口管理器               在android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:424)               在android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:350)               在android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:116)               在android.app.Dialog.dismissDialog(Dialog.java:362)               在android.app.Dialog.dismiss(Dialog.java:345)               在com.blazapps.allenfamilymedicine.SendReportActivity $ SendMailTask​​.onPostExecute(SendReportActivity.java:168)               at com.blazapps.allenfamilymedicine.SendReportActivity $ SendMailTask​​.onPostExecute(SendReportActivity.java:138)               在android.os.AsyncTask.finish(AsyncTask.java:651)               在android.os.AsyncTask.-wrap1(AsyncTask.java)               在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:668)               在android.os.Handler.dispatchMessage(Handler.java:102)               在android.os.Looper.loop(Looper.java:148)               在android.app.ActivityThread.main(ActivityThread.java:5417)               at java.lang.reflect.Method.invoke(Native Method)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:1)

我仍然不知道为什么会抛出异常,但因为我在EmailStatusResponse中保存了大量信息,我可以用try catch包围异常,如果任务完成,我得到了我期望的结果。

try {
    weakReference.get().dismiss();
} catch (Exception e) {}

如果有人能找到更好的解决方案,我会非常感激。我讨厌捕捉异常。通常有更好的方法。