Thread in Android
<p>I have an example like this:   </p>

<pre><code>package android.uiexample;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ProgressBar;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class BasicViewsExampleActivity  extends Activity 
{

private static int progress = 0;
private ProgressBar progressBar;
private int progressStatus = 0;
private Handler handler = new Handler();

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.basic_views);

    progressBar = (ProgressBar) findViewById(R.id.progressbar);

    //---do some work in background thread---
    new Thread(new Runnable() 
    {
        public void run() 
        {
            //---do some work here---
            while (progressStatus &lt; 10) 
            {
                progressStatus = doSomeWork();
            }



            //---hides the progress bar---
            handler.post(new Runnable() 
            {
                public void run() 
                {
                    progressBar.setVisibility(View.GONE);
                }
            });

        }    

        //---do some long lasting work here---
        private int doSomeWork() 
        {
            try {
                //---simulate doing some work---
                Thread.sleep(500);
            } catch (InterruptedException e) 
            {
                e.printStackTrace();
            }
            return ++progress;
        }

    }).start();
}
</code></pre>

<p>}</p>

<p>In this example, it use Handler to post a Runable to exc progressBar.setVisibility(View.GONE);. I don't know why i can't call progressBar.setVisibility(View.GONE); directly:</p>

<pre><code>            //---do some work here---
            while (progressStatus &lt; 10) 
            {
                progressStatus = doSomeWork();
            }



            //---hides the progress bar---
            progressBar.setVisibility(View.GONE);
</code></pre>

<p>Anybody can tell me why i can't?</p>
