Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the Can't create handler inside thread that has not called Looper.prepare () problem when using Handler

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to solve the problem of Can't create handler inside thread that has not called Looper.prepare () when using Handler, which has a certain reference value. Interested friends can refer to it. I hope you can learn a lot after reading this article.

In android development, the main thread cannot perform time-consuming operations, so we often put the time-consuming operations in the child thread, so we need the child thread and the main thread to communicate with each other, so we need to use handler.

In the process of using handler, if you are not proficient in using handler, you will occasionally have an error exception of Can't create handler inside thread that has not called Looper.prepare (). As mentioned in the previous blog post on the principles of Handler, the use of Handler relies on the Looper loop to send messages, and if you do not create a Looper object, the message cannot be sent and the system will crash. So why does handler sometimes work and sometimes it doesn't?

In fact, if you create a handler in the main thread, the system will automatically create the Looper, but when you create the handler in the child thread, the Looper will not be created automatically. If you do not create the Looper manually, the system will crash.

An example is as follows: first, create a Handler in the child thread (that is, the main thread sends messages to the child thread)

The system will report an error exception for Can't create handler inside thread that has not called Looper.prepare ().

The solution is:

Create a Looper object through Looper.prepare ();

Let's create a Handler in the main thread

When a Handler is created in the main thread (that is, the child thread sends messages to the main thread processing), the system automatically creates the Looper instead of manually creating the Looper.

Thank you for reading this article carefully. I hope the article "how to solve the Can't create handler inside thread that has not called Looper.prepare () problem when using Handler" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report