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

情到浓时终转凉″ 提交于 2019-12-11 18:43:24

问题


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.


回答1:


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




回答2:


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();


来源:https://stackoverflow.com/questions/10497854/only-the-original-thread-that-created-a-view-hierarchy-can-touch-its-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!