android:取消进度条捕获异常:android.view.ViewRoot$CalledFromWrongThreadException [英] android : cancelling progressbar caught exception : android.view.ViewRoot$CalledFromWrongThreadException

查看:46
本文介绍了android:取消进度条捕获异常:android.view.ViewRoot$CalledFromWrongThreadException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用程序中实现一个进度条.该进度条在按钮单击时可见,然后一个线程也随之启动.当线程完成时,我试图使进度条不可见.但它在执行时遇到异常:

I am trying to implement a progressbar in my application. This progressbar is visible on button click, and then a thread is also starting along with it. when the thread completes, I am trying to make invisible the progressBar. But it caught exception on excuting :

progressbar.setVisibility(View.GONE);

progressbar.setVisibility(View.GONE);

例外:

android.view.ViewRoot$CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图.

我的代码是:

  Thread thredUpdate   = new Thread()
  {
     public void run()
     {
        ClearDatabase();
                 progressbar.setVisibility(View.GONE);                                      
     }
  };
  requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
  setProgressBarVisibility(true);
  ProgressBar progressbar  = (ProgressBar)findViewById(R.id.progress);
  progressbar .setVisibility(View.GONE);
  btnUpdateDB.setOnClickListener(new OnClickListener()
  {
      @Override
      public void onClick(View argBtnUpdateDB)
      {                 
            setProgressBarVisibility(true);
            progressbar.setVisibility(View.VISIBLE);
             try
            {
                     if((progressbar.isEnabled()))
                    thredUpdate.start();
             }               
            catch(Exception exSoap) 
            {                                                                                 
                   exSoap.printStackTrace();
            }

  } });

如何在不调用 setVisibility(View.GONE) 的情况下取消此进度条?

How can I cancel this progressbar without calling setVisibility(View.GONE)?

谢谢...

推荐答案

你应该从 UI 线程调用它:

You should call it from UI thread:

runOnUiThread(new Runnable() {
        public void run() {
             progressbar.setVisibility(View.GONE);
        }
    });

另一种方法是使用 AsyncTask 而不是常规的 Thread.

Another approach is to use AsyncTask instead of regular Thread.

这篇关于android:取消进度条捕获异常:android.view.ViewRoot$CalledFromWrongThreadException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆