我正在尝试在计时器中循环烤面包,但是烤面包没有显示
logcat中的日志显示无法在未调用looper.prepare()的线程内创建处理程序,我不确定这意味着什么
int initialDelay = 10000;
int period = 10000;
final Context context = getApplicationContext();
TimerTask task = new TimerTask()
{
public void run()
{
try
{
if (a != "")
{
Toast toast = Toast.makeText(context, "Alert Deleted!", Toast.LENGTH_SHORT);
toast.show();
}
}
catch (Exception e)
{
}
}
};
timer.scheduleAtFixedRate(task, initialDelay, period);
我的应用程序所做的是每隔10秒就会检查某个变量是否为空。如果为空,则将显示吐司。
我在服务类中做到这一点没有问题,但是当我尝试将其实现为
public void onCreate(Bundle savedInstanceState)
我得到这个错误
最佳答案
您是从辅助线程调用它的。您需要从主线程内调用Toast.makeText()(以及其他大多数处理UI的函数)。例如,您可以使用处理程序。
看到这个答案...。
Can't create handler inside thread that has not called Looper.prepare()