为什么在子线程中直接new Handler会报错?

来源:互联网 发布:淘宝免单群教程 编辑:程序博客网 时间:2024/04/28 06:10

为什么在子线程中直接new Handler会报错:

我们从报错开始追踪:“Can’t create handler inside thread that has not called Looper.prepare()”
—-》
错误日志就来自Handler的默认构造方法,源码如下:

/**     * Default constructor associates this handler with the queue for the     * current thread.     *     * If there isn't one, this handler won't be able to receive messages.     */    public Handler() {        if (FIND_POTENTIAL_LEAKS) {            final Class<? extends Handler> klass = getClass();            if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&                    (klass.getModifiers() & Modifier.STATIC) == 0) {                Log.w(TAG, "The following Handler class should be static or leaks might occur: " +                    klass.getCanonicalName());            }        }        mLooper = Looper.myLooper();        if (mLooper == null) { //当mLooper为null时就会报这个异常            throw new RuntimeException(                "Can't create handler inside thread that has not called Looper.prepare()");        }        mQueue = mLooper.mQueue;        mCallback = null;    }

—-》
那么我看下myLooper这个方法:

/**     * Return the Looper object associated with the current thread.  Returns     * null if the calling thread is not associated with a Looper.     */    public static Looper myLooper() {        return sThreadLocal.get();    }

很明显这里只是从sThreadLocal这个当前线程的对象身上获取到一个Looper。
—-》
那么我们需要知道这个Looper是什么时候被赋值到当前线程的对象上面的,Looper类中有一个prepare方法

/** Initialize the current thread as a looper.      * This gives you a chance to create handlers that then reference      * this looper, before actually starting the loop. Be sure to call      * {@link #loop()} after calling this method, and end it by calling      * {@link #quit()}.      */    public static void prepare() {        prepare(true);    }    private static void prepare(boolean quitAllowed) {        if (sThreadLocal.get() != null) {            throw new RuntimeException("Only one Looper may be created per thread");        }        sThreadLocal.set(new Looper(quitAllowed));//在这里创建一个Looper对象并赋值给当前线程    }

—-》
那么这个Looper.prepare()方法是什么时候在哪里调用的呢?
当我们在主线程中创建Handler对象的时候没有问题,是因为主线程会自动调用Looper.prepare()方法去
给当前主线程创建并设置一个Looper对象,随意在Handler构造函数中从当前线程的对象身上拿到这个Looper。
但是子线程中并不会自动调用这个方法,所以要想在子线程中创建Handler对象就必须在创建之前手动调用Looper.prepare()方
法,否则就会报错。

1 0
原创粉丝点击