Android: AsyncTask ProgressDialog will not open in ActivityGroup

眉间皱痕 提交于 2019-12-09 04:39:19

问题


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.


回答1:


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());



回答2:


Simple here you can also use following

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

it work perfectly for me .




回答3:


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.



来源:https://stackoverflow.com/questions/4329710/android-asynctask-progressdialog-will-not-open-in-activitygroup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!