Error: Only the original thread, created a view hierarchy can touch its views--handler use

Source: Internet
Author: User

In the following tutorial learning to display the Web page of the HTML source code times wrong: Only the original thread, created a view hierarchy can touch it views, through the Internet to find information:

The associated view and controls in Android are not thread-safe and must be handled separately. If you want to update the view, you must update it in the main thread, and you cannot perform the updated operation on the child thread.

In this case, we can implement an update to the UI through the handler object.

official description of handler:

A Handler allows you to send and process and Message Runnable objects associated with a thread ' s MessageQueue . Each Handler instance are associated with a single thread and that thread ' s message queue. When you create a new Handler, it's bound to the thread/message queue of the thread that's creating it--from that PO int on, it'll deliver messages and runnables to that message queue and execute them as they come out of the message Queu E

. Handler Use occasions:

1, to schedule messages and runnables to being executed as some point in the future;

Schedule messages and runnables to be executed at a later point in time.

2, to enqueue an action to being performed on a different thread than your own.

The action is queued for execution in a different thread. That is, you can implement inter-thread communication. For example, when you create a child thread, you can then get the handler object created in the parent thread from your child thread, and you can send a message to the parent thread's message queue through that object. Because Android requires the interface to be updated in the UI thread, you can update the interface in other threads with this method.

To update a UI instance through handler:

Steps:

1. Create the Handler object (this is created in the main thread for easy updating of the UI).

2, constructs the Runnable object, updates the interface in the runnable.

3. Update the UI by post,runnable the object to the UI thread in the child thread's Run method.

protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_html);p HTML=(EditText) Findviewbyid (r.id.phtml); Button1=(Button) Findviewbyid (r.id.check_html); ihtml=(TextView) Findviewbyid (r.id.ihtml);//create a handler that belongs to the main threadHandler=NewHandler (); Button1.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View v) {NewThread () { Public voidrun () {Try{String path=Phtml.gettext (). toString (); sHTML=htmlservice.gethtml (path);} Catch(Exception e) {//TODO auto-generated Catch blocke.printstacktrace ();} Handler.post (RUNNABLEUI); }}.start (); //new Htmlsourceasync (ihtml). Execute (path);}});}//build runnable objects and update the interface in RunnableRunnable runnableui=NewRunnable () {@Override Public voidrun () {//Update InterfaceIhtml.settext (shtml);} };

Part of the article quoted from: http://blog.csdn.net/djx123456/article/details/6325983

Error: Only the original thread, created a view hierarchy can touch its views--handler use

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.