`
iaiai
  • 浏览: 2179791 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

 
阅读更多
分析:
报的是一个Android Looper的错误,从字面意思上看是没有调用Looper.prepare()方法。

Looper是什么?

Looper又是什么呢? ,其实Android中每一个Thread都跟着一个Looper,Looper可以帮助Thread维护一个消息队列。默认的Thread是没有Looper机制的,需要在线程中首先调用Looper.prepare()来创建消息队列,然后调用Looper.loop()进入消息循环。
分享到:
评论

相关推荐

    开新线程引发的常见3个异常

    开一个新线程,引起的异常。1. ANR: application not responding 2.android.view.ViewRoot$...3.java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

    Android开发中Looper.prepare()和Looper.loop()

    在Android开发中,`Looper`、`Looper.prepare()` 和 `Looper.loop()` 是处理线程间通信和异步任务的关键组件。它们与`Message`和`Handler`一起构成了Android的消息传递机制。默认情况下,一个线程并没有内置的消息...

    Android 高级开发面试题以及答案整理

    当在非UI线程(子线程)中创建`Handler`时,如果没有调用`Looper.prepare()`,程序将抛出异常:“Can't create handler inside thread that has not called Looper.prepare()”。这是因为`Handler`依赖于`Looper`来...

    Android+高级开发面试题以及答案整理.pdf

    在创建 Handler 对象时,如果不在主线程中创建,而是在子线程中创建,会导致程序崩溃,提示的错误是 Can't create handler inside thread that has not called Looper.prepare()。这是因为子线程中没有调用 Looper....

    阿里、腾讯等一线互联网大厂Android 高级开发面试题以及答案整理(1307页).pdf

    当创建一个新的Handler实例时,如果在子线程中没有事先调用Looper.prepare()来初始化Looper,会抛出“Can't create handler inside thread that has not called Looper.prepare()”异常。这是因为Handler需要一个...

    Android 异步消息处理机制 让你深入理解 Looper、Handler、Message三者关系 - Hongyang -

    "Can't create handler inside thread that has not called Looper.prepare()"); } mQueue = mLooper.mQueue; ... } ``` 3. **Message** Message是消息的载体,用于在Handler和Looper之间传递数据。它包含了...

    线程消息传送机制源码

    1. 在创建Handler时,如果当前线程没有Looper,会抛出`java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()`。这说明Handler必须在有Looper的线程中使用,例如...

    Handler + Thread + Looper

    在Android应用开发中,Handler、Thread和Looper是三个至关重要的组件,它们共同构成了Android线程间通信的基础架构。本文将详细解析这三个概念及其相互关系,旨在帮助开发者理解它们的工作原理和实际应用场景。 ...

    Android线程模式(handler,thread,looper)

    本文将深入探讨Android中的三种主要线程模式:Handler、Thread以及Looper,并结合源码分析它们的工作原理。 首先,我们来理解一下Android应用的基本运行环境。Android系统默认在主线程(UI线程)中执行所有的用户...

    Android Looper使用介绍

    在 run() 方法中,我们首先调用 Looper.prepare() 方法来创建 Looper 对象,然后创建一个 Handler 对象,并将其与 Looper 对象关联。最后,我们调用 Looper.loop() 方法来处理消息循环。 通过使用 Looper 对象,...

    Handler与AsyncTask,Looper使用示例

    handler.sendMessage(msg); Looper.loop(); // 保持子线程的消息循环 } }).start(); // 使用AsyncTask的例子 class MyTask extends AsyncTask, Integer, String> { @Override protected String doInBackground...

    安卓Android源码——HandlerLooper2.rar

    在安卓开发中,`Handler`、`Looper`和`MessageQueue`是三个核心组件,它们共同构成了Android消息处理机制,用于实现线程间的通信。这个压缩包“安卓Android源码——HandlerLooper2.rar”可能包含了关于这些组件的...

    Thread,Looper,Handler,Message,MessageQueue,MessagePool之间的关系

    这些组件之间的交互过程大致如下:子线程中的Handler通过Looper.prepare()和Looper.loop()建立消息循环,然后使用Message.obtain()创建Message,设置数据和目标Handler,最后通过Handler.sendMessage()将Message放入...

    handler与looper及messagequeue的简单总结

    在Android开发中,Handler、Looper和MessageQueue是用于实现线程间通信的关键组件,它们共同构建了一个消息处理机制。理解这三个概念以及它们之间的关系对于优化应用程序的性能和响应性至关重要。 Handler(处理器...

    应用源码之HandlerLooper2.zip

    在Android系统中,`Handler`、`Looper`和`MessageQueue`是三个核心组件,它们共同构成了Android消息处理机制,使得应用程序能够实现线程间的通信和异步任务处理。本资料"应用源码之HandlerLooper2.zip"显然是针对这...

    Handler+Looper+MessageQueue

    `CustomThread` 类继承自 `Thread` 并重写了 `run()` 方法,在 `run()` 中调用了 `Looper.prepare()` 和 `Looper.loop()`,使线程具备了处理消息的能力。`mHandler` 是在主线程中创建的 Handler 实例,它用来发送...

    Android单线程模型中Message、Handler、Message Queue、Looper之间的关系---附实例源码

    Handler获取当前线程中的looper对象,looper用来从存放Message的MessageQueue中取出Message,再有Handler进行Message的分发和处理. 简单定义: 1、Message Queue(消息队列): 用来存放通过Handler发布的消息,通常...

    Android应用源码之HandlerLooper1.zip

    3. `Message`的创建和发送:通过`Handler.sendMessage()`或`Handler.sendMessageDelayed()`发送消息,可能还有使用`Message`携带数据的示例。 4. 异步任务的实现:可能包含一个使用`Handler`、`Looper`完成异步任务...

    线程与Looper

    handler.sendMessage(msg); ``` 5. **处理消息**:Handler的`handleMessage()`方法会在消息被分发到对应线程时被调用,此时可以执行相应操作。 在实际应用中,我们通常结合使用Handler和Looper来实现异步处理,...

Global site tag (gtag.js) - Google Analytics