java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

时间:2023-03-09 00:35:27
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

1. 程序时,出现运行时异常,如上图所示。

2.异常原因分析。在ContentObserver的onChange方法中,调用了Toast.makeText方法。onChange方法应该在子线程运行,在android中的子线程中不能直接控制UI组件,否则就会报异常

3.Looper类别用来为一个线程开启一个消息循环。默认情况下Android中新诞生的线程是没有开启消息循环的。(主线程除外,主线程系统会自动为其创建Looper对象,开启消息循环)