Two ways to update the UI for Android--handler and Runonuithread (

Source: Internet
Author: User

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{&nbsp ;        @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;}&LT ;/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 ();               }               });     

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.