title: Threading Problem with Java and Android
body: <p>I'm designing an app, which flicks through a series of pictures, like flicking through a photo album. Pretty standard stuff I'm sure. 
  Since the picture must be viewed for a few seconds before it automatically changes ot the next pic, I decided to ake a thread that shows the pic, waits  couple of second and then moves on.</p>
  
  <pre><code>Picthread(ImageView Image1) {
          this.image = Image1;
  
      }
  
      public void run(){
  
          showPicture(image);
          animal_array = new String[7];
  
          while (counter &lt; 7){
              try{
  
  
                  int timer = 0;
  
                  while (timer &lt; 2000){
  
                      sleep(500);
  
                      timer+=500;
                  }
                  image.post(new Runnable(){
  
                      public void run() {
  
                           showPicture(image);
  
  
                      }
  
                  });
  
  
  
              }
               catch (InterruptedException e) {  
                  } 
          }
  
  
  
  
      }
  </code></pre>
  
  <p>This actualy worked. showPictures is a very simple method that just chooses a picture and puts it on an ImageView. It isn't necessary to know about it for my problem.
  At first itdidn't work, the logcat said I couldn't touch a view on a view heirarchy that wasnt created in this thread. I wasn't sure what that meant so I did the</p>
  
  <pre><code>image.post(...)
  </code></pre>
  
  <p>code. Which worked. My main question is: Why was this necesary? If you look at my above code, the first showPicture() method is not inside the image.post() code. But no exception is registered. I don't understand this, why isn't a post needed? But also why do I need to post, since Image is a class variable, and I thought could be viewed by all threads. I was happy it worked, but puzzled.</p>
  
  <p>Please bare in mind, this is my first attempt at threading in Java on anything more than trivial textbook examples. SO I'm still pretty confused.</p>
  
  <p>By the way, in the end I ditched the whole thread, and just did</p>
  
  <pre><code>new Thread(new Runnable() {
              public void run() {...}
  </code></pre>