关于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的相关

很多网友在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...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

解决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

今天在做ViewPager广告切换的时候,抱着尝试的方法想用单独的线程去控制自动切换。尝试失败后忘记改掉线程,并于新加了一张图片时候报这个错误,加图片之前正常,但是程序运行逻辑异常,加了图片之后此错误...

关于“Only the original thread that created a view hierarchy can touch its views.”异常的处理

原因:android中相关的view和控件不是线程安全的,我们必须单独做处理。这里借此引出Handler的使用。 Handler的官方描述: A Handler allows you to s...

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——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——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——Handler的使用

今天写了一个更新UI的小例子,没想到出了log打印了这样一个错误:Only the original thread that created a view hierarchy can touch it...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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