The error is quite explicit. To update a single visual object, you must run the changes inside the main thread. A quick and dirty fix can cause the update code inside runOnUiThread () .
However, in your case, I would use AsyncTask to download and update the progress of the progress bar. A task has the property of starting a user interface thread at its completion (so that you can update views there, for example, reject the execution dialog)
Here's an example of using AsyncTask to display a download progress dialog box.
Update
Stackoverflow already has answers to all your questions. Here's an AsyncTask example for loading some content and displaying the loading process. Just what you want.
Update 2
Ok, here is your code using AsyncTask:
public class Activity1 extends Activity { private ProgressDialog progressDialog; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); new AsyncTask<Integer, Integer, Boolean>() { ProgressDialog progressDialog; @Override protected void onPreExecute() { progressDialog = ProgressDialog.show(Activity1.this, "", "Loading..."); } @Override protected Boolean doInBackground(Integer... params) { if (params == null) { return false; } try { Thread.sleep(params[0]);
source share