Android:AsyncTask ProgressDialog 将不会在 ActivityGroup 中打开
我试图在轮询我的服务器时打开一个进度对话框。该类是一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果 ActivityGroup 位于 TabActivity 中,则您有超过两层的嵌套活动。 Android 目前不支持此功能,但有一个解决方法。您必须将父活动传递给对话框。
在活动类中为此目的创建一个辅助方法:
然后将该行更改
为
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:
Then change the line
to
这里很简单,您也可以使用以下方法,
它对我来说非常有效。
Simple here you can also use following
it work perfectly for me .
如果 getParent() 不适合您,请尝试仅使用
TabsActivity.context
(或替换父选项卡活动类的名称)。我正在使用嵌套活动,因此使用 getParent() 仍然没有返回对话框的正确上下文,因为它需要扩展 TabsActivity 的活动的上下文,而不是直接父级。简单修复:
您需要在 TabsActivity 类中创建一个上下文变量。类似于 onCreate 方法中的
public static TabsActivity context;
和context=this
。替换创建对话框的这一行:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
与:
它就像一个魅力。
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:
You'll need to create a context variable in the TabsActivity class. Something like
public static TabsActivity context;
andcontext=this
in the onCreate method.Replace this line where you create the dialog:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
With:
and it works like a charm.