Toast 和 Looper
Toast 和 Looper,一个属于 android.widget,一个属于 android.os,两个貌似联系不怎么紧密的类,却通过下面这个异常联系到了一起:
/** * Return the Looper object associated with the current thread. Returns * null if the calling thread is not associated with a Looper. */public static final Looper myLooper() { return (Looper)sThreadLocal.get();}以上就是 myLooper() 方法的真实面貌,通过注释可以看出问题的真正原因在于当前线程并没有绑定 Looper,返回为 null 是正确但非正常的结果。
问题的根本原因已经解开,但是另外一个疑团也就产生了:为何当前线程没有 Looper 呢?经过对代码进行 review,原因找到了:当事者在 non-UI 线程进行 Toast.makeText