title: How to automatically Click a Button in Android after a 5 second delay
body: <p>I have a small Android application that automatically clicks the button after 5 seconds. I have used <code>performClick();</code> but this does not work. When the timer gets to zero it simply crashes.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
      try {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.local);
          getActionBar().setIcon(R.drawable.menu_drop);
  
          buttonClick();
  
          Thread timer = new Thread(){
              public void run(){ 
                  try{
                      sleep(5000);
                  } catch (InterruptedException e){
                      e.printStackTrace();
                  }finally{
                      button1.performClick();
                  }
              }
          };
          timer.start();
      } catch (NotFoundException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  
  public void buttonClick() {
      button1 = (Button) findViewById(R.id.button);
      button1.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
              Intent i = new Intent(TestButton2.this, LocationView.class);
              startActivity(i);
          } 
      }); 
  }
  </code></pre>