众所周知,在 Thread( 线程 )中,直接更新布局控件的内容是不被允许的,当今天出现了一个状况,我在 Handle 中更新碎片的 TextView 内容竟然给我抱了哥错:Only the original thread that created a view hierarchy can touch its views
因为非UI线程做了UI操作,进而报出了 Only the original thread that created a view hierarchy can touch its views。
安排 messages 和 runnables 在将来的某个时间点执行 和将action入队以备在一个不同的线程中执行。也就是说,将更新 UI 的待爱吗写在一个 Runnable 中,然后调用 Handle 的 post 方法,就能使线程更新。范例:
private Handler handler = new Handler();
private Thread thread = new Thread(){
@Override
public void run() {
while (true){
handler.post(runnable);
try {
sleep(60000);// 没一分钟看下事件
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
private Runnable runnable = new Runnable() {
@Override
public void run() {
mTips.setText("已打卡: " + time + " 分钟");
}
};com/example/joker/signinsystem/fragment/personal.java --> 点击跳转 —— https://github.com/FishInWater-1999/android-SignInSystem/blob/master/app/src/main/java/com/example/joker/signinsystem/fragments/Personal.java
欢迎关注我获得跟多小姿势~~~