在我的活动中,有一个异步任务连接到互联网并将数据保存在我的数据库中。当我在此任务运行时更改方向时,我会得到一个异常:
07-27 16:01:45.956 10173-10173/de.MayerhoferSimon.Vertretungsplan E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalArgumentException: View not attached to window manager
at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:668)
at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:364)
at android.view.WindowManagerImpl$CompatModeWrapper.removeView(WindowManagerImpl.java:163)
at android.app.Dialog.dismissDialog(Dialog.java:347)
at android.app.Dialog.dismiss(Dialog.java:330)
at de.MayerhoferSimon.Vertretungsplan.MainActivity$1.onPostExecute(MainActivity.java:129)
at de.MayerhoferSimon.Vertretungsplan.MainActivity$1.onPostExecute(MainActivity.java:90)
at android.os.AsyncTask.finish(AsyncTask.java:631)
所以这就是我的 AsyncTask 的样子:
@Override
protected void onPreExecute() {
isRefreshing = true;
this.dialog.setMessage("Lädt Daten...");
this.dialog.show();
}
@Override
protected void onPostExecute(String successful) {
if (!isFinishing()) {
if (successful.equals("succeed")) {
Toast toast = Toast.makeText(context, R.string.toast_refresh_succeed, Toast.LENGTH_LONG);
toast.show();
}
else if (successful.equals("failed")) {
Toast toast = Toast.makeText(context, R.string.toast_refresh_failed, Toast.LENGTH_LONG);
toast.show();
}
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}
}
我现在想要的是在orientationchange 上设置一个延迟,这样就不会再发生这种情况,并且只有在AsyncTask 完成时方向才会改变。