我有一个活动,它将启动AsyncTask并在操作期间显示进度对话框。声明该活动不能通过旋转或键盘滑动来重新创建。
<activity android:name=".MyActivity"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation"
>
<intent-filter>
</intent-filter>
</activity>
任务完成后,我关闭对话框,但是在某些电话(框架:1.5、1.6)上会引发此类错误:
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)
我的代码是:
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();
根据我已阅读(http://bend-ing.blogspot.com/2008/11/properly-handle-progress-dialog-in.html)并在Android资料中看到的内容,看来这是唯一可能的解决方法例外是活动被破坏时。但是正如我已经提到的,我禁止在基本活动中进行活动娱乐。
因此,任何建议都将不胜感激。
一只萌萌小番薯
相关分类