【发布时间】:2020-07-15 15:29:16
【问题描述】:
发现网上有人有类似问题here。
这不起作用:
Timer t = new Timer(false);
t.schedule(new TimerTask() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "msg", Toast.LENGTH_LONG).show();
}
}, 5000);
但是如果我在 timertask 之外实例化 toast 然后在 run 中显示它,它就可以工作。
我觉得可能跟所谓的UI线程有关,
但究竟如何?
【问题讨论】:
-
您不能使用 Toast 或从非 UI 线程进行任何 UI 更新。
-
@Eng.Fouad,还有更多细节吗?
-
查看this page 的末尾,特别是上面写着:
Make sure that you access the Android UI toolkit only on the UI thread. -
@Eng.Fouad,我正在寻找更多内部原因
标签: android