java - Android Ui 未在线程中更新
问题描述
在日志中正常工作,但在 setText 中不会更改屏幕上的任何内容,并且不会发生错误。
//within the onCreate method
myRef.child(fireUser.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
millis = Long.valueOf(dataSnapshot.child("millis").getValue().toString());
t1.run();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Intent i = new Intent(getApplicationContext(), SplashActivity.class);
startActivity(i);
finish();
}
});
//Out of the onCreate method
Thread t1 = new Thread(){
@Override
public void run() {
try {
long i;
for(i=System.currentTimeMillis(); i < millis+millis_wait; i=System.currentTimeMillis()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
long pr = (millis + millis_wait) - System.currentTimeMillis();
tx_time.setText(""+pr);
Log.i("TST", "" + pr);
}
});
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
我应该改变什么?
正如我所说,代码正在运行,正在调用并完美执行 Log 事件,唯一没有发生的是更新 setText 根本没有运行,也没有任何错误。
解决方案
调用t1.start();
你的 UI 线程来运行它
推荐阅读
- php - 修复 PHP 中的图像旋转
- sql-server - 如何在 WHERE 语句中使用 Case 或 Coalesce 检查列是否为 NULL
- linux - 命令显示正确,但执行方式不同
- node.js - 如何将多个 ReadableStreams 通过管道传输到单个 WriteStream?
- visual-studio - Visual Studio 解决方案资源管理器 - 观察到资产文件的不同图标
- php - 如何格式化关联数组
- java - 如何在 Java 中使用 ta-lib 修复 RSI 计算输出中的零点?
- laravel - 使用重定向从另一个控制器方法调用 Laravel 控制器方法
- amazon-web-services - 如何使用 AWS 设置 no-www?
- java - IntelliJ 配置中的命名策略映射标记是什么意思?