Two ways to update the UI for Android--handler and Runonuithread ()
In the Android development process, it is often necessary to update the UI of the interface. The update UI is to be updated by the main thread, which is the UI thread update. If you update the page directly in a thread other than the main thread, the error often appears. Throw exception: Android.view.viewroot$calledfromwrongthreadexception:only The original thread that created a view hierarchy can tou CH its view. Only the thread that originally created this view hierarchy (view hierachy) can modify it's views (view) without saying much, post the following code method one: in Activity.oncreate (Bundle Savedinstancestate) Creates an instance of the handler class that invokes the function displayed in the update interface in the Handlemessage callback function of the handler instance. interface: [HTML] <span style= "FONT-SIZE:14PX;" >public class Mainactivity extends Activity { private EditText uitxt; Private B Utton updateuibtn; Private Uihandler Uihandler; @Override public void onCreate (Bundle savedinstancestate) { & nbsp super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Uitxt = (EditText) Findviewbyid (r.id.ui_txt); UPDATEUIBTN = (Button) Findviewbyid (R.ID.UPDATE_UI_BTN); Updateuibtn.setonclicklistener (new View.onclicklistener () { public void OnClick (View v) { &nbs P //TODO auto-generated method stubs &NBS P Uihandler = new Uihandler (); uithread thread = new Uithread (); Thread.Start (); } }); @Override public boolean oncreateoptionsmenu ( Menu menu) { getmenuinflater (). Inflate (R.menu.activity_main, menu); return true; } Private class Uihandler extends handler{ @Override &NBSP;&N Bsp public void Handlemessage (Message msg) { //TODO Auto -generated method Stub super.handlemessage (msg); Bundle bundle = Msg.getdata (); String color = bundle.getstring ("color"); uitxt.settext (color); } } Private class Uithread extends thread{  ; @Override public void run () { &N Bsp try { Thread.Sleep (+); } catch (InterruPtedexception e) { //TODO auto-generated catch block e.printstacktrace (); } Message msg = new Messag E (); BUNDLE bundle = new bundle (); bundle.putstring ("Color", "yellow"); Msg.setdata (bundle); MainActivity.this.UIhandler.sendMessage (msg); } &NBSP;}< ;/span> Post-Update: Www.2cto.com method Two: Use Activity.runonuithread (Runnable) to create the code that updates the UI in Runnable, and then, when you need to update the UI, Pass this Runnable object to Activity.runonuithread (Runnable). This way, the runnable can be called in the UI program. If the current thread is the UI thread, then the action is performed immediately. If the current thread is not a UI thread, the action is a UI thread published to the event queue [htmL] fusionfield.currentactivity.runonuithread (new Runnable () { & nbsp; public void run () { Toast.maketext (Getapplicationcontext (),, "Update My UI, Toast.length_long). s how (); } });