记录使用AsyncTask遇到的问题(8.9更新,已解决)

解决方法写在这篇博客《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。不知如何解决。只好弃坑用别的写法试试。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值