当 AsyncTask 使用小部件时 Activity 关闭
我有一个启动 AsyncTask 的活动。此 AsyncTask 在 onPostExecute 中使用 AutoCompleteTextView.showDropDown()。问题是当 AsyncTask 运行时,我按“后退”,应用程序在 showDropDown() 行抛出异常,因为 AsyncTask 仍在使用 AutoCompleteTextView。如何解决这个问题? (最好的方法)
更新:我对sonykuba的解决方案仍然不满意。使用非最终静态变量时这不是一个好习惯。我想将 AsyncTask 与 Activity 解耦,但想不出更好的解决方案。
更新:添加例外
09-05 13:41:05.536: ERROR/AndroidRuntime(962): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
09-05 13:41:05.536: ERROR/AndroidRuntime(962): at android.view.ViewRoot.setView(ViewRoot.java:505)
09-05 13:41:05.536: ERROR/AndroidRuntime(962): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
09-05 13:41:05.536: ERROR/AndroidRuntime(962): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
09-05 13:41:05.536: ERROR/AndroidRuntime(962): at android.view.Window$LocalWindowManager.addView(Window.java:424)
09-05 13:41:05.536: ERROR/AndroidRuntime(962): at android.widget.PopupWindow.invokePopup(PopupWindow.java:828)
09-05 13:41:05.536: ERROR/AndroidRuntime(962): at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:740)
09-05 13:41:05.536: ERROR/AndroidRuntime(962): at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1207)
09-05 13:41:05.536: ERROR/AndroidRuntime(962): at com.megadict.business.recommending.RecommendTaskInitializer$2.onPostExecute(RecommendTaskInitializer.java:58)
09-05 13:41:05.536: ERROR/AndroidRuntime(962): at com.megadict.business.recommending.AbstractRecommendTask.onPostExecute(AbstractRecommendTask.java:29)
09-05 13:41:05.536: ERROR/AndroidRuntime(962): at com.megadict.business.recommending.RecommendTask.onPostExecute(RecommendTask.java:37)
I have an Activity which starts an AsyncTask. This AsyncTask uses AutoCompleteTextView.showDropDown() in onPostExecute. The problem is when AsyncTask is running, I press "Back" and the application throws exception at showDropDown() line because AsyncTask is still using AutoCompleteTextView. How to fix this problem? (the best way)
Update: I'm still not satisfied with the solution of sonykuba. It is not a good practice when using non-final static variable. I want to decouple AsyncTask from the Activity but can't think out any better solutions for this.
Update: Add exception
09-05 13:41:05.536: ERROR/AndroidRuntime(962): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
09-05 13:41:05.536: ERROR/AndroidRuntime(962): at android.view.ViewRoot.setView(ViewRoot.java:505)
09-05 13:41:05.536: ERROR/AndroidRuntime(962): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
09-05 13:41:05.536: ERROR/AndroidRuntime(962): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
09-05 13:41:05.536: ERROR/AndroidRuntime(962): at android.view.Window$LocalWindowManager.addView(Window.java:424)
09-05 13:41:05.536: ERROR/AndroidRuntime(962): at android.widget.PopupWindow.invokePopup(PopupWindow.java:828)
09-05 13:41:05.536: ERROR/AndroidRuntime(962): at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:740)
09-05 13:41:05.536: ERROR/AndroidRuntime(962): at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1207)
09-05 13:41:05.536: ERROR/AndroidRuntime(962): at com.megadict.business.recommending.RecommendTaskInitializer$2.onPostExecute(RecommendTaskInitializer.java:58)
09-05 13:41:05.536: ERROR/AndroidRuntime(962): at com.megadict.business.recommending.AbstractRecommendTask.onPostExecute(AbstractRecommendTask.java:29)
09-05 13:41:05.536: ERROR/AndroidRuntime(962): at com.megadict.business.recommending.RecommendTask.onPostExecute(RecommendTask.java:37)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的方法是使用某种静态布尔值,并在 Activity 类的 onPause 中将 TRUE 赋予它 onResume 并将 FALSE 赋予它。然后将if语句放在onPostExecute中。
The easiest way is to have some kind of static boolean and put TRUE to it onResume and FALSE in onPause of Activity class. Then put if statement in onPostExecute.
我有一个解决方案。 AsyncTask的所有者有一个函数:
当我需要使用它时,我会检查它是否为null。如果不是,则调用showDropDown()。
当 Activity 销毁时,我将其设置为 null。
I had a solution. The owner of AsyncTask has a function:
When I need to use it, I will check if it is null. If not, call showDropDown().
When Activity destroys, I set it to null.