Only the original thread that created a view hierarchy can touch its views
Asked Answered
D

2

0

Im Adding Progress Dialog in some Activity .But im getting Exception mention in title.how to resolve it.

dialog = ProgressDialog.show(Notification.this, "loading please wait",                
"Loading. Please wait...", true);

new Thread() {
 public void run() {
   try{
     performBackgroundProcess1();
     //sleep(3000,000);
   } catch (Exception e) {
     Log.e("tag", e.getMessage());
 }

 // dismiss the progress dialog
     dialog.dismiss();
  }
}.start();

Any thing wrong with this.all Background process is performed in performbackgroundprocess method.

Dominic answered 8/5, 2012 at 11:37 Comment(0)
W
0

You cant call dialog.dismiss(); in the background thread. You can make Threads send messages to handlers when they are done and in the handler you can dismiss the dialog. Handlers work in ui thread

There is a tutorial about it

Waldman answered 8/5, 2012 at 11:39 Comment(3)
How can i implement handler?im using it first time.Dominic
In this im calling API and parsing its response in performBackgroundProcess1();method.its not clear how to call this method .should be other example.Dominic
after performBackgroundProcess1(); you should send message to the handler you create. Please first check the link i sent please.Waldman
Y
0

use runOnUiThread as:

        new Thread() {
     public void run() {
       try{
         performBackgroundProcess1();
         //sleep(3000,000);
       } catch (Exception e) {
         Log.e("tag", e.getMessage());
     }

// dismiss the progress dialog
      CurrentActivity.this.runOnUiThread(new Runnable(){  
    @Override  
    public void run() {  
    // TODO Auto-generated method stub  
    dialog.dismiss();  
    }  
    });    
      }
    }.start();
Yi answered 8/5, 2012 at 12:2 Comment(1)
but @Nitesh it's not possible to access UI elements in thread directly so you must go for runOnUiThread,Handler,or HandlerThread for upading or accessing UI elementsExtroversion

© 2022 - 2024 — McMap. All rights reserved.