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

原创 2016年08月28日 20:57:33
今晚写进度条的时候用到了线程,想在子线程里面更新UI组件,给我报了一个

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


百度后已解决,记录下来作为备忘

在Android中ui组件不是线程安全的,因此只能在主线程里更新,得使用到Handler机制

代码如下

public class MainActivity extends AppCompatActivity {
    private Button tiao = null;
    private Button yuan = null;
    private ProgressDialog p = null;
    int iCon = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tiao = (Button)super.findViewById(R.id.tiao);
        yuan = (Button)super.findViewById(R.id.yuan);

        tiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                p = new ProgressDialog(MainActivity.this);
                p.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                p.setTitle("正在下载");
                p.setIcon(R.drawable.b);
                p.setProgress(10);
                p.setIndeterminate(false);
                p.setCancelable(true);

                //让进度条显示
                p.show();
                Thread t1 = new Thread(){
                    @Override
                    public void run() {
                        try {
                            while (iCon <= 100) {
                                // 由线程来控制进度。
                                p.setProgress(iCon++);
                                Log.d("fwk----",Integer.toString(iCon));
                                Thread.sleep(500);
                                Message msg = new Message();
                                msg.what = iCon;
                                handler.sendMessage(msg);
                            }
                            p.cancel();
                        }catch(InterruptedException e){
                            e.printStackTrace();
                        }
                    }
                };
       }
    });

}
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            p.setMessage(iCon+"%");
     }
};



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

错误:Only the original thread that created a view hierarchy can touch its views——Handler的深入解析

这个错误很常见,基本上写线程操作都遇到过这个错误。根本原因是view控件的线程安全问题,通俗点讲就是所有的更新UI操作都需要在主线程(也就是UI线程中完成),而不能在新开的子线程中操作。 基本思...
  • zsg2063
  • zsg2063
  • 2014年05月12日 16:45
  • 1254

在工作线程中处理UI线程报错:Only the original thread that created a view hierarchy can touch its views.

情景在okhttpclient onSuccess()方法中 更新UI,报错原文如果将更新UI控件的代码写入到工作线程中便会报错, 例子如下:Runnable socketRun = new R...

错误:Only the original thread that created a view hierarchy can touch its views——Handler的使用

今天写了一个更新UI的小例子,没想到出了log打印了这样一个错误:Only the original thread that created a view hierarchy can touch it...

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

很多网友在Android中使用多线程处理UI相关内容时可能会发现Logcat提示Only the original thread that created a view hierarchy can t...

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

很多网友在Android中使用多线程处理UI相关内容时可能会发现Logcat提示Only the original thread that created a view hierarchy can t...

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

解决Only the original thread that created a view hierarchy can touch its views 很多网友在Android...

Only the original thread that created a view hierarchy can touch its views. 是怎么产生的

我们都知道,在Android里面,只有主线程(MainThread)才可以更新ui,比如设置TextView的文本内容,ImageView的图片等等。 只要我们在非主线程去操作ui界面,就是抛出"On...

浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views.

Only the original thread that created a view hierarchy can touch its views.
  • Mars2639
  • Mars2639
  • 2011年07月22日 11:05
  • 28255

Only the original thread that created a view hierarchy can touch its views错误相关

很多网友在Android中使用多线程处理UI相关内容时可能会发现Logcat提示Only the original thread that created a view hierarchy can t...

android中用线程更新UI,报错only the original thread that created a view hierarchy can touch its views

android开发中难免要更新UI,比如做一个时钟,
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:关于Only the original thread that created a view hierarchy can touch its views.
举报原因:
原因补充:

(最多只允许输入30个字)