java - How to properly implement android.os.Handler class instead of Timer in Android application? -


so wanted implement timer in anroid program , found out better way using handler class.

first decided write simplest program using handler - text set after 1 second. i'm totall beginner in android, went through tutorials on web 1 http://developer.android.com/resources/articles/timed-ui-updates.html , still application shows error ("application mtimer stopped").

so point out making mistake? gratefull, here's code:

 public class mtimer extends activity {      textview tv;     button button1,button2;     handler mhandler;      private runnable mytask = new runnable() {            public void run() {                 tv.settext("text");             }         };      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.main);          button1=(button)findviewbyid(r.id.button1);         tv=(textview)findviewbyid(r.id.textview1);   button1.setonclicklistener(new view.onclicklistener() {              @override             public void onclick(view v) {                  mhandler.postdelayed(mytask, 100);             }         });       } } 

you should initialize handler in oncreate method @ least code mhandler = new handler();.

note, mytask task run on ui thread, since handler declared there

api docs handler.postdelayed:

the runnable run on thread handler attached.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -