Update UI controls in the main thread in new Android threads

Source: Internet
Author: User

View in Android is not thread-safe, so if you update a UI control in the main thread directly in a new one, the following error is reported:
Android.view.viewrootimpl$calledfromwrongthreadexception:only the original thread that created a view hierarchy can Touc H its Views.

In order to solve the problem of updating UI controls in another thread, we can use several of the following solutions:
1. Write your own handler solution, see the use of handler in Android.
2. Using the Activity#runonuithread (Runnable) method, which receives a Runnable object as a parameter, we need to update the view in the Runnalble run method, which also uses handler inside the method.
3. Use the Postxxx method in view:
View#post (Runnable)
View#postdelayed (Runnable, long)
View#postdelayed (Runnable action, long Delaymillis)
These methods are also to receive runnable objects as parameters, but also in the Runnable run method to update the view, these methods are also implemented internally with handler.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Update UI controls in the main thread in new Android threads

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.