cant create handler inside thread that has not cal

2019-07-18 03:17发布

问题:

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

回答1:

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....

        }
    });