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

2023-12-09 19:48

本文主要是介绍关于Only the original thread that created a view hierarchy can touch its views.,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

今晚写进度条的时候用到了线程,想在子线程里面更新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.的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/474868

相关文章

ScrollView 往上滑动,里面的一个View停在某个位置的思路

1.scrollView的contentoffset 为view的左上角,减去此时scrollView的左上角 2.而且还不需要让那个红色的view removeFromSuperView ,直接self.view AddSubView 就会自动从原来的那个View脱离开来 3.以后遇到问题的思路。当发现UIView很许多奇特的效果的时候,思考它是不是在不断的改变父控件。 #pragma m

导航条下 ScrollView 第一个View去掉向下偏移的64px

self.automaticallyAdjustsScrollViewInsets=NO;      self.scrollView.contentInset=UIEdgeInsetsMake(-64, 0, 0, 0); self.automaticallyAdjustsScrollViewInsets=NO; 相同的意思。

android自定义View的和FramgentActivity的一个小坑

对于自定义View //加载样式TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBarView, defStyleAttr, 0);setTitle(typedArray.getString(R.styleable.TitleBarView_main_title));//不能写成

jmeter之Thread Group(线程组)

Thread Group(线程组) 1.线程组,或者可以叫用户组,进行性能测试时的用户资源池。 2.是任何一个测试计划执行的开始点。 3.上一篇提到的“控制器”和“HTTP请求”(采集器)必须在线程组内;监听器等其他组件,可以直接放在测试计划下。 线程组设置参数的意义 我们以下图为例,进行详细说明。见下图:  区域1(在取样器错误后要执行的动作) 这个区域的主要作用很明显,在线程内

Exception processing async thread queue Exception processing async thread queue

错误信息描述: eclipse 在debug中弹出异常信息框 Exception processing async thread queue Exception processing async thread queue 解决方法: eclipse 中有一个Expressions窗口,里面添加的 expression 没有正确执行,并且没有删除,只要 Remove All Expressio

no thread-bound request found:are you referring to request

问题描述: 通过webservice接口调用程序时,发现在执行查询的时候一直报一个错误,错误信息如下: java.lang.IllegalStateExceptino:No thread-bound request found:are you referring to request attributes outside of an actual web request,or processi

Creating custom and compound Views in Android - Tutorial(翻译)

Creating custom and compound Views in Android - Tutorial(翻译) 译前的: 之前做了三篇学习笔记,从知乎上面看到了这篇英文的推荐,总的来说可以是一篇导读,没有相关的学习,看这篇,可以作为一个学习脉络导向;有相关的学习底子,可以作为一个基础夯实、思维理清。没想到一翻译就是四个多小时…英语渣,很多词句都不太准确,幸好有之前的学习基础打底…

Android自定义View学习笔记03

Android自定义View学习笔记03 参考gitHub上面的开源项目CircleImageView 预备知识 BitMap类 BitMap位图类,其中有一个嵌套类叫Bitmap.Config,内部有四个枚举值。这个类的作用是定义位图存储质量,即存储一个像素的位数,以及是否能显示透明、半透明颜色(Possible bitmap configurations. A bitmap co

Android自定义view学习笔记02

Android自定义view学习笔记02 本文代码来自于张鸿洋老师的博客之Android 自定义View (二) 进阶 学习笔记,对代码进行些许修改,并补充一些在coding过程中遇到的问题、学习的新东西。 相关代码 //CustomImageView.javapackage mmrx.com.myuserdefinedview.textview;import android.con

Android自定义view学习笔记01

Android自定义view学习笔记01 昨天看博客的时候看到鸿洋老师的博客里面有关于自定义view的学习教程。一直深感所掌握的东西太少太杂,按照他的Android 自定义View (一)所讲内容,代码实践。根据实际情况稍作修改,并且补充一些在代码过程中知识点,做此笔记。 相关代码 //CustomView01.javapackage mmrx.com.myuserdefinedvi