title: Refresh TextView in Android
body: <p>I try to refresh TextView in Android. I am using ExecutorService in my code. When I pushed the "Start refresh" button, after several seconds my app has been stopped by Android system. I have read several topics in stackoverflow.com, but I still cannot understand what I am doing wrong. Here is my code:</p>
  
  <p>MainActivity.java:</p>
  
  <pre><code>import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.TextView;
  import java.util.Calendar;
  import java.util.GregorianCalendar;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  import java.util.concurrent.TimeUnit;
  
  public class MainActivity extends Activity {
      private static ExecutorService exec;
      private static TextView textView;
      private static Calendar cal;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          textView = (TextView) findViewById(R.id.text_field);
          cal = new GregorianCalendar();
          exec = Executors.newSingleThreadExecutor();
      }
  
      private static class Task implements Runnable {
          @Override
          public void run() {
              try {
                  while(!Thread.currentThread().isInterrupted()) {
                      textView.setText(cal.getTime().toString());
                      TimeUnit.SECONDS.sleep(1);
                  }
              }
              catch (InterruptedException e) {
                  return;
              }
          }
      }
  
      public void finish_refresh(View view) {
          exec.shutdownNow();
      }
  
      public void start_refresh(View view) {
          exec.submit(new Task());
  
      }
  }
  </code></pre>
  
  <p>activity_main.xml:</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".MainActivity"
      android:orientation="vertical"&gt;
  
      &lt;Button android:layout_width="200dp"
          android:layout_height="50dp"
          android:text="@string/start_refresh"
          android:onClick="start_refresh"/&gt;
  
      &lt;Button android:layout_width="200dp"
          android:layout_height="50dp"
          android:text="@string/finish_refresh"
          android:onClick="finish_refresh"/&gt;
  
      &lt;TextView android:layout_height="200dp"
          android:layout_width="200dp"
          android:layout_marginTop="10dp"
          android:id="@+id/text_field" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>