当 AsyncTask 使用小部件时 Activity 关闭

发布于 2024-12-01 13:54:42 字数 1833 浏览 0 评论 0原文

我有一个启动 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

许仙没带伞 2024-12-08 13:54:42

最简单的方法是使用某种静态布尔值,并在 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.

む无字情书 2024-12-08 13:54:42

我有一个解决方案。 AsyncTask的所有者有一个函数:

public void setSearchBar(final AutoCompleteTextView searchBar) {
    this.searchBar = searchBar;
}
  • 当我需要使用它时,我会检查它是否为null。如果不是,则调用showDropDown()。

  • 当 Activity 销毁时,我将其设置为 null。

owner.setSearchBar(null);

I had a solution. The owner of AsyncTask has a function:

public void setSearchBar(final AutoCompleteTextView searchBar) {
    this.searchBar = searchBar;
}
  • 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.

owner.setSearchBar(null);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文