Android: ProgressBar in AsyncTask
-
Got ProgressBar in AsyncTask, but he's out.
@Override public void onCreate(Bundle savedInstanceState) { ... showList(); ... }
private void showList() { TextTask task = new TextTask(getApplicationContext()); task.execute(Utils.OpenFile(nameOfFile, this)); } class TextTask extends AsyncTask<String, Void, Void> { ProgressDialog mProgressDialog; Context context; public TextTask(Context c) { context = c; } @Override protected void onPreExecute() { mProgressDialog = ProgressDialog.show (context , "Loading" , "", true); mProgressDialog.setCancelable(false); mProgressDialog.show(); super.onPreExecute(); } @Override protected Void doInBackground(String... text_task) { *** } @Override protected void onPostExecute(Void result) { mProgressDialog.dismiss(); ....
Mistake:
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:550)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:302)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:216)
at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:141)
at android.app.Dialog.show(Dialog.java:278)
at android.app.ProgressDialog.show(ProgressDialog.java:116)
at android.app.ProgressDialog.show(ProgressDialog.java:99)
at com...ShoppingList.BlistViewer$TextTask.onPreExecute(BlistViewer.java:364)
-
Substitution
TextTask task = new TextTask(getApplicationContext());
♪
TextTask task = new TextTask(this);
The parameter for any Dialog should not be just a context, that is, Activity