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