1

I am making a project in which i am using the progressdialog and i want to show this progress dialog on creation of activity and i am able to that. In the on create method i want the image to be invisible and i want image to be visible after completion of progress dialog but it is throwing exception in the line imagevisible();

The logcat is:

04-12 12:48:35.309: E/AndroidRuntime(4994):     at com.example.project1.ShowPassword$waiter.run(ShowPassword.java:59)

Code

        ImageView iv;

        ProgressDialog p;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.showpass);

            iv=(ImageView)findViewById(R.id.imageView1);
            iv.setVisibility(View.INVISIBLE);

            p= new ProgressDialog(this);
            p.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            p.setTitle("Getting Password: ");
            p.setMessage("Loading:");
            p.setMax(100);
            p.show();
            Thread t=new Thread(new waiter());
            t.start();

        public class waiter extends Thread{         

        public void run(){

            for(int i=0; i<5; i++){
            p.incrementProgressBy(20);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }p.dismiss();
        imagevisible();

        }
    }

    public void imagevisible(){
        iv.setVisibility(View.VISIBLE);
    }
Hariharan
  • 24,741
  • 6
  • 50
  • 54
nawaab saab
  • 1,892
  • 2
  • 20
  • 36

2 Answers2

1

You can't change UI from non UI thread. You can use runOnUiThread method of Activity:

public class waiter extends Thread{
    public void run(){

        //...

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                imagevisible();
            }
        });
    }
}
eleven
  • 6,779
  • 2
  • 32
  • 52
0

What you want is an AsyncTask. Implement doInBackground() (runs in the background) and onPostExecute() (runs on the UI thread).

https://developer.android.com/reference/android/os/AsyncTask.html

Karakuri
  • 38,365
  • 12
  • 84
  • 104