title: How to Update Textview text in loop
body: <p>I want to have an extremely simple XML layout with just one textview in it. I want the text view to go up by one every second. To do this I have used this code</p>
  
  <pre><code>    public class test extends Activity{
      TextView num;
      int to =0;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
          setContentView(R.layout.updatexml);
  num = (TextView) findViewById(R.id.tvUpdate);
          startIncrementing();
      }
      public void startIncrementing() {
          while (to &lt; 100) {
              num.setText(to + "");
              to++;
          }
          }
      }
  </code></pre>
  
  <p>Have done a good hour of searching but have not found a good answer. Any help would be great.</p>
  
  <p>Edit:</p>
  
  <p>Used given answers below and got this in the error log:</p>
  
  <pre><code>04-15 17:35:17.637: E/AndroidRuntime(28985): FATAL EXCEPTION: main
  04-15 17:35:17.637: E/AndroidRuntime(28985): Process: com.infitenothing.dogcat, PID: 28985
  04-15 17:35:17.637: E/AndroidRuntime(28985): java.lang.NullPointerException
  04-15 17:35:17.637: E/AndroidRuntime(28985):    at com.infitenothing.word.Test2$1.run(Test2.java:41)
  04-15 17:35:17.637: E/AndroidRuntime(28985):    at android.os.Handler.handleCallback(Handler.java:733)
  04-15 17:35:17.637: E/AndroidRuntime(28985):    at android.os.Handler.dispatchMessage(Handler.java:95)
  04-15 17:35:17.637: E/AndroidRuntime(28985):    at android.os.Looper.loop(Looper.java:136)
  04-15 17:35:17.637: E/AndroidRuntime(28985):    at android.app.ActivityThread.main(ActivityThread.java:5017)
  04-15 17:35:17.637: E/AndroidRuntime(28985):    at java.lang.reflect.Method.invokeNative(Native Method)
  04-15 17:35:17.637: E/AndroidRuntime(28985):    at java.lang.reflect.Method.invoke(Method.java:515)
  04-15 17:35:17.637: E/AndroidRuntime(28985):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
  04-15 17:35:17.637: E/AndroidRuntime(28985):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
  04-15 17:35:17.637: E/AndroidRuntime(28985):    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
  04-15 17:35:17.637: E/AndroidRuntime(28985):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Current Code</p>
  
  <pre><code>public class Test2 extends Activity {
  
      // Init var
      int i=0;
      TextView num;
      final Handler myHandler = new Handler();
      final Timer myTimer = new Timer();
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          // Init your TextView
          num = (TextView) findViewById(R.id.tvUpdate);
  
          // Timer
          TimerTask myTask = new TimerTask() {
              public void run() {
                  updateUI(); // updateUI method
              }
          };
          myTimer.schedule(myTask,0,1000); // TimerTask, delay, period
      }
  
      // Runnable method
      final Runnable myRunnable = new Runnable() {
          public void run() {
              num.setText(String.valueOf(i)); // update your text
          }
      };
  
      // updateUI method related to a Runnable
      private void updateUI() {
          if(i &lt; 100) {
              i++;
              // num.setText(String.valueOf(i)); = avoid the RunTime error
              myHandler.post(myRunnable); // relate this to a Runnable
          } else {
              myTimer.cancel(); // stop the timer
              return;
          }
      }
  }
  </code></pre>