2

Note : I know there are many questions related to this, but still I am not convince, so asking.

I am getting cant create handler inside thread that has not called looper.prepare when I try to show the dialog.

Here is my code...

//this method is called from a different method based on some condition which is inturn called on click a button

 private void download() {
    thread = new Thread() {
     public void run() {
    /**** Downloads each tour's Tour.plist file ****/
            try {
                // do many heavy operations here, like download, 
                //calling web webvice and starting another activity

               This comes at the end

                Intent toAudio = new Intent(TourDescription.this,Audio.class);
                startActivity(toAudio);
         } catch (Exception e) {
       }  

       }
    }; 
    thread.start();
 }

Now before this actity gets called I am trying to show a dialog. I am trying to place that just before calling Intent.

Can any body please tell me how to do this, as I am not understanding how to solve this

tejas
  • 2,435
  • 10
  • 37
  • 54

1 Answers1

5

you cannot show a dialog from a child thread. A dialog can only be showed from within the UI thread/main Thread.

try this from inside the child thread

runOnUiThread(new Runnable() {

        @Override
        public void run() {
            // TODO show dialog....

        }
    });
Umesh
  • 4,406
  • 2
  • 25
  • 37
  • Great, this helped me. And I understood the concept also...:) – tejas Dec 02 '11 at 05:44
  • Glad i could help you...consider marking the answer as solved so that others can find it easily. – Umesh Dec 02 '11 at 05:46
  • But I have a small problem here, dialog comes almost at the end of start actiivty, I want the dialog to be appeared little early than now. Do you have answer for this? – tejas Dec 02 '11 at 06:04
  • There is a slight delay between calling a dialog and its appearance. That is the time android takes to create and show the dialog. you are calling startActvity immediately after the dialog call, that is the reason why this is happening. add a slight delay using a Handler before calling "startActivity(toAudio);" – Umesh Dec 02 '11 at 06:10