1.未在主线程创建AsyncTask
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at com.cutt.zhiyue.android.service.HuaWeiPushBroadcastReceiver.commitClientId(SourceFile:141)
at com.cutt.zhiyue.android.service.HuaWeiPushBroadcastReceiver.onToken(SourceFile:89)
at com.huawei.hms.support.api.push.PushReceiver$a.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:831)
原因:是在简网项目中使用了华为推送,在继承自华为的PushReceiver的子类HuaWeiPushBroadcastReceiver中,复写了
public void onToken(Context context, String token, Bundle bundle)
方法,可是官方文档没有说这个方法是调用在非UI线程,导致我们误以为是在主线程上调用。结果我们在这个方法中做了new AsyncTask的动作,才会爆出上述错误。
同时,无意间搜索出一篇文章,在此向作者致敬学习。你真的了解AsyncTask?