title: Accessing UI elements in Activity from another class
body: <p>I have a problem with that code</p>
  
  <pre><code>setContentView(R.layout.game);
  TextView text = (TextView) findViewById(R.id.qwe_string);
  text.setText("Hello Android");
  </code></pre>
  
  <p>It works if it's inside an activity, but if it isn't, obviously, it gives errors:</p>
  
  <pre><code>The method findViewById(int) is undefined for the type new TimerTask(){}
  The method setContentView(int) is undefined for the type new TimerTask(){}
  </code></pre>
  
  <p>The code is inside a timer in separate class (not activity). The full code is as follows.</p>
  
  <pre><code>//main timer task for ingame time processing
  static Timer gameTimer = new Timer();
  static TimerTask gameTimerTask = new TimerTask() {
      @Override
      public void run() {
          setContentView(R.layout.game);
          TextView text = (TextView) findViewById(R.id.qwe_string);
          text.setText("Hello Android");
      }
  };
  </code></pre>
  
  <p>I tried changing it like that</p>
  
  <pre><code>ScreenGame.this.setContentView(R.layout.game);
  TextView text = (TextView) ScreenGame.this.findViewById(R.id.qwe_string);
  text.setText("Hello Android");
  </code></pre>
  
  <p>But it still doesn't work :(</p>
  
  <p><em>PS - Yes, I searched other similar questions, but all of them although appear to be the same, in fact completely different.</em></p>