安卓:从onRestoreInstanceState调用时AsynchTask失败() [英] Android: AsynchTask fails when called from onRestoreInstanceState()

查看:161
本文介绍了安卓:从onRestoreInstanceState调用时AsynchTask失败()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,谁的用户界面需要一点工作,以建立(必须处理一些东西)。所以,我建立使用AsynchTask(它显示一个进度对话框,同时做一些后台处理,然后进行UI更新)的用户界面。我希望能够把这种AsynchTask从OnRestoreInstanceState(),这样我就可以重建一个旋转的用户界面。然而,当我打电话MyAsynchTask.execute()从onRestorInstanceState()方法中,我得到了以下异常:

 十二月一日至五日:28:45.194:ERROR /窗口管理器(409):android.view.WindowLeaked:活动com.MyActivity渗漏窗口com.android.internal.policy.impl .PhoneWindow $ DecorView @ 43e70368最初此处添加
12月1日至五日:28:45.194:ERROR /窗口管理器(409):在android.view.ViewRoot< INIT>(ViewRoot.java:247)
12月1日至五日:28:45.194:ERROR /窗口管理器(409):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
12月1日至五日:28:45.194:ERROR /窗口管理器(409):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
12月1日至五日:28:45.194:ERROR /窗口管理器(409):在android.view.Window $ LocalWindowManager.addView(Window.java:424)
12月1日至五日:28:45.194:ERROR /窗口管理器(409):在android.app.Dialog.show(Dialog.java:241)
12月1日至五日:28:45.194:ERROR /窗口管理器(409):在com.MyClass $ MyAsynchTask.on preExecute(MyClass.java:428)
 

解决方案

第1步:让你的的AsyncTask 静态内部类或常规完整的Java类

步骤#2:手动活动的任务应该通过构造函数的参数,在任务中的数据成员举行工作相关联

第三步:返回从 onRetainNonConfigurationInstance任务对象()

第四步:在的onCreate()新活动实例,通过获取现有任务对象getLastNonConfigurationInstance()并手动重新关联的活动的任务应该通过一个se​​tter方法​​。

第四步:如果需要,修改 onPostExecute()不引用活动

下面是展示这种流动的示例项目。

I have an app who's UI takes a little work to build (has to process some stuff). So, I build the UI using an AsynchTask (it shows a progress dialog while doing some background processing, then does the UI update). I would like to be able to call this AsynchTask from OnRestoreInstanceState(), so that I can rebuild the UI on a rotation. However, when I call MyAsynchTask.execute() from the onRestorInstanceState() method, I get the below exception:

01-05 12:28:45.194: ERROR/WindowManager(409): android.view.WindowLeaked: Activity com.MyActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@43e70368 that was originally added here
01-05 12:28:45.194: ERROR/WindowManager(409):     at android.view.ViewRoot.<init>(ViewRoot.java:247)
01-05 12:28:45.194: ERROR/WindowManager(409):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
01-05 12:28:45.194: ERROR/WindowManager(409):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
01-05 12:28:45.194: ERROR/WindowManager(409):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
01-05 12:28:45.194: ERROR/WindowManager(409):     at android.app.Dialog.show(Dialog.java:241)
01-05 12:28:45.194: ERROR/WindowManager(409):     at com.MyClass$MyAsynchTask.onPreExecute(MyClass.java:428)

解决方案

Step #1: Make your AsyncTask a static inner class or a regular full Java class.

Step #2: Manually associate the activity the task should work with via a constructor parameter, held in a data member of the task.

Step #3: Return the task object from onRetainNonConfigurationInstance()

Step #4: In onCreate() of the new activity instance, get the existing task object via getLastNonConfigurationInstance() and manually re-associate the activity the task should work with via a setter method.

Step #4: If needed, modify onPostExecute() to not reference the activity.

Here is a sample project demonstrating this flow.

这篇关于安卓:从onRestoreInstanceState调用时AsynchTask失败()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆