java.lang.IllegalArgumentException:如果查看没有连接到窗口管理器 [英] java.lang.IllegalArgumentException: View not attached to window manager
问题描述
我有开始的AsyncTask,并给出了操作的持续时间进度对话框的活动。该活动被宣布不会被旋转或键盘滑盖重建。
I have an activity that starts AsyncTask and shows progress dialog for the duration of operation. The activity is declared NOT be recreated by rotation or keyboard slide.
<activity android:name=".MyActivity"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation"
>
<intent-filter>
</intent-filter>
</activity>
在任务完成后,我dissmiss对话框,但在某些手机上(框架:1.5,1.6),这样的错误被抛出:
Once task completed, I dissmiss dialog, but on some phones (framework: 1.5, 1.6) such error is thrown:
java.lang.IllegalArgumentException: View not attached to window manager
at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:356)
at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:201)
at android.view.Window$LocalWindowManager.removeView(Window.java:400)
at android.app.Dialog.dismissDialog(Dialog.java:268)
at android.app.Dialog.access$000(Dialog.java:69)
at android.app.Dialog$1.run(Dialog.java:103)
at android.app.Dialog.dismiss(Dialog.java:252)
at xxx.onPostExecute(xxx$1.java:xxx)
我的code是:
My code is:
final Dialog dialog = new AlertDialog.Builder(context)
.setTitle("Processing...")
.setCancelable(true)
.create();
final AsyncTask<MyParams, Object, MyResult> task = new AsyncTask<MyParams, Object, MyResult>() {
@Override
protected MyResult doInBackground(MyParams... params) {
// Long operation goes here
}
@Override
protected void onPostExecute(MyResult result) {
dialog.dismiss();
onCompletion(result);
}
};
task.execute(...);
dialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface arg0) {
task.cancel(false);
}
});
dialog.show();
这是我看过(<一href="http://bend-ing.blogspot.com/2008/11/properly-handle-progress-dialog-in.html">http://bend-ing.blogspot.com/2008/11/properly-handle-progress-dialog-in.html)而在看到Android的来源,它看起来像唯一可能的情况下获得活动时被破坏的例外是,但正如我刚才所说,我不许基本活动活动消遣。
From what I have read (http://bend-ing.blogspot.com/2008/11/properly-handle-progress-dialog-in.html) and seen in Android sources, it looks like the only possible situation to get that exception is when activity was destroyed. But as I have mentioned, I forbid activity recreation for basic events.
因此,任何建议都非常AP preciated。
So any suggestions are very appreciated.
推荐答案
我也收到此错误的有时当我拒绝来自onPostExecute方法对话框,并完成活动。我想有时候活动被完成之前成功对话框驳回。
I too get this error sometimes when I dismiss dialog and finish activity from onPostExecute method. I guess sometimes activity gets finished before dialog successfully dismisses.
简单,对我的作品还没有有效的解决方案。
Simple, yet effective solution that works for me
@Override
protected void onPostExecute(MyResult result) {
try {
if ((this.mDialog != null) && this.mDialog.isShowing()) {
this.mDialog.dismiss();
}
} catch (final IllegalArgumentException e) {
// Handle or log or ignore
} catch (final Exception e) {
// Handle or log or ignore
} finally {
this.mDialog = null;
}
}
这篇关于java.lang.IllegalArgumentException:如果查看没有连接到窗口管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!