解决方法写在这篇博客《Can't create handler inside thread Thread that has not called Looper.prepare()的解决》https://blog.csdn.net/weixin_42694582/article/details/81535083
在service中调用task,重写了doInBackground方法如下
@Override
protected Integer doInBackground(TimeSetted... timeSetteds) {
TimeSetted timeSetted = timeSetteds[0];
long millIsInFuture = timeSetted.getTotalMill();
lastProgress = millIsInFuture;
timer = new Timer(millIsInFuture,TimeSetted.SECOND_TO_MILL);
try{
while (getLastProgress() > 0){
if (isCanceled){
timer.cancel();
return TYPE_CANCELED;
}else if (isPaused){
timer.pause();
return TYPE_PAUSED;
}else {
timer.start();
long progress = timer.getmMillisUntilFinished();
publishProgress(progress);
}
}
return TYPE_FINISHED;
}catch (Exception e){
e.printStackTrace();
}
return TYPE_CANCELED;
}
报错如下
java.lang.RuntimeException: Can't create handler inside thread Thread[AsyncTask #1,5,main] that has not called Looper.prepare()
在网上找了一下,该错误大部分情况是由于在子线程中操作UI导致出错。但是我一没有更新UI,二没有创建handler。不知如何解决。只好弃坑用别的写法试试。