Android:AsyncTask ProgressDialog 将不会在 ActivityGroup 中打开

发布于 2024-10-05 21:51:57 字数 998 浏览 0 评论 0原文

我试图在轮询我的服务器时打开一个进度对话框。该类是一个 ActivityGroup,因为它嵌套在选项卡栏中。为了将视图保持在框架内,需要 ActivityGroup。这是我的 ActivityGroup 类的声明:

   public class CheckInActivity extends ActivityGroup{
        ...
        public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.checkin);
            new LocationControl().execute(this);

现在我的 AsyncTask 类位于同一个 CheckInActivityClass 中,如下所示:

private class LocationControl extends AsyncTask<Context, Void, Void>
    {
        private final ProgressDialog dialog = new ProgressDialog(CheckInActivity.this);

        protected void onPreExecute()
        {
            this.dialog.setMessage("Determining your location...");
            this.dialog.show();
        }

当我运行给定的应用程序时,它会出现与 WindowManager$BadTokenException 相关的错误。说明它无法使用未知令牌启动窗口。我尝试制作一个示例应用程序,它只是一个常规活动(而不是 ActivityGroup),它工作得很好。

有谁知道如何修改它以使其工作,或者允许进度条嵌套在选项卡栏中的解决方法?非常感谢任何帮助。

I am trying to have a a progress dialog open when polling my server. The class is an ActivityGroup because it is nested within a tab bar. To keep the view within the frame, the ActivityGroup is needed. Here is the declaration of my ActivityGroup class:

   public class CheckInActivity extends ActivityGroup{
        ...
        public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.checkin);
            new LocationControl().execute(this);

Now my AsyncTask class is within the same CheckInActivityClass as such:

private class LocationControl extends AsyncTask<Context, Void, Void>
    {
        private final ProgressDialog dialog = new ProgressDialog(CheckInActivity.this);

        protected void onPreExecute()
        {
            this.dialog.setMessage("Determining your location...");
            this.dialog.show();
        }

When I run the given app it throughs an error relating to WindowManager$BadTokenException. Stating the it cannot start the window with an unknown token. I tried making a sample app that is just a regular Activity(not ActivityGroup) and it worked just fine.

Does anyone know how to modify this to make it work, or a work around that will allow the progress bar to be nested within the tab bar? Any help is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

天荒地未老 2024-10-12 21:51:57

如果 ActivityGroup 位于 TabActivity 中,则您有超过两层的嵌套活动。 Android 目前不支持此功能,但有一个解决方法。您必须将父活动传递给对话框。

在活动类中为此目的创建一个辅助方法:

private Context getDialogContext() {
    Context context;
    if (getParent() != null) context = getParent();
    else context = this;
    return context;
}

然后将该行更改

private final ProgressDialog dialog = new ProgressDialog(CheckInActivity.this);

private final ProgressDialog dialog = new ProgressDialog(getDialogContext());

If the ActivityGroup is within a TabActivity you have nested activities with more then two levels. Android doesn't support this at the moment but there is a workaround. You have to pass the parent activity to the dialog.

Create a helper method for this purpose in the activity class:

private Context getDialogContext() {
    Context context;
    if (getParent() != null) context = getParent();
    else context = this;
    return context;
}

Then change the line

private final ProgressDialog dialog = new ProgressDialog(CheckInActivity.this);

to

private final ProgressDialog dialog = new ProgressDialog(getDialogContext());
爱要勇敢去追 2024-10-12 21:51:57

这里很简单,您也可以使用以下方法,

private final ProgressDialog dialog = new ProgressDialog(getParent());

它对我来说非常有效。

Simple here you can also use following

private final ProgressDialog dialog = new ProgressDialog(getParent());

it work perfectly for me .

那支青花 2024-10-12 21:51:57

如果 getParent() 不适合您,请尝试仅使用 TabsActivity.context (或替换父选项卡活动类的名称)。我正在使用嵌套活动,因此使用 getParent() 仍然没有返回对话框的正确上下文,因为它需要扩展 TabsActivity 的活动的上下文,而不是直接父级。

简单修复:

  1. 您需要在 TabsActivity 类中创建一个上下文变量。类似于 onCreate 方法中的 public static TabsActivity context;context=this

  2. 替换创建对话框的这一行:

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

与:

AlertDialog.Builder builder = new AlertDialog.Builder(TabsActivity.context); 

它就像一个魅力。

If getParent() doesn't work for you, try using just TabsActivity.context (or substitute the name of your parent tab activity class). I am using nested activities and as a result using getParent() is still not returning the right context for the dialog, since it needs the context of the activity extending TabsActivity, not the immediate parent.

Simple fix:

  1. You'll need to create a context variable in the TabsActivity class. Something like public static TabsActivity context; and context=this in the onCreate method.

  2. Replace this line where you create the dialog:

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

With:

AlertDialog.Builder builder = new AlertDialog.Builder(TabsActivity.context); 

and it works like a charm.

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