title: How to call Button.performClick in Android JUnit test case?
body: <p>I'm new to Android Testing stuff. I'd like to test if clicking a button will open up a corresponding activity or not. I did some research and found out that I will need to use ActivityManager to do the checking.</p>
  
  <p>Problem is, I can't get the "clicking" part working. I'm trying to use <code>Button.performClick()</code>.</p>
  
  <p>Initially I just called this function, and got an error saying that I can't do this in the current thread. After some googling I found out that I need to call it in the UI thread, and came across <code>runOnUiThread(Runnable r) method</code>.</p>
  
  <p>The button I'm trying to click is <code>_helloButton_</code>. This is obtained in the <code>_setUp()_ method</code>. I did <code>_assertNotNull_</code> check on this to make sure it's there.</p>
  
  <p>Within a test method, I call</p>
  
  <pre><code>mActivity.runOnUiThread(new Runnable() {
          public void run() {
              helloButton.requestFocus();
          }
      });
  helloButton.performClick();
  </code></pre>
  
  <p>and I get a NPE at the line calling <code>requestFocus()</code>.</p>
  
  <p>Next I tried </p>
  
  <pre><code>mActivity.runOnUiThread(new Runnable() {
          public void run() {
              helloButton.performClick();
          }
      });
  </code></pre>
  
  <p>and still get the same null pointer exception.</p>
  
  <p>On the JUnit perspective, I get this message</p>
  
  <pre><code>Test failed to run to completion. Reason: 'Instrumentation run failed due to 'java.lang.NullPointerException''. Check device logcat for details
  </code></pre>
  
  <p>And the stackTrace look like this.</p>
  
  <pre><code>    08-05 19:03:11.922: ERROR/AndroidRuntime(578): Uncaught handler: thread main exiting due to uncaught exception
  08-05 19:03:11.922: ERROR/AndroidRuntime(578): java.lang.NullPointerException
  08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at com.example.helloworldmk2.test.HelloWorldMK2Test$1.run(HelloWorldMK2Test.java:57)
  08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at android.os.Handler.handleCallback(Handler.java:587)
  08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at android.os.Handler.dispatchMessage(Handler.java:92)
  08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at android.os.Looper.loop(Looper.java:123)
  08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at android.app.ActivityThread.main(ActivityThread.java:4363)
  08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at java.lang.reflect.Method.invokeNative(Native Method)
  08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at java.lang.reflect.Method.invoke(Method.java:521)
  08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
  08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
  08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Line 57 is where I'm calling the <code>helloButton.performClick()</code>.</p>
  
  <p>I'm not sure why I'm getting NPE; assertNotNull passes without problem. If you can help me with this issue, I'd really appreciate it. Thanks in advance.</p>
  
  <p>EDIT: I'm subclassing ActivityInstrumentationTestCase2 for this particular test class.</p>
  
  <p>EDIT2: Logcat spews out some errors before NPE happens.</p>
  
  <p>I see </p>
  
  <p>08-05 20:08:54.702: ERROR/AndroidRuntime(754): ERROR: thread attach failed</p>
  
  <p>and </p>
  
  <p>08-05 20:08:58.642: ERROR/gralloc(52): [unregister] handle 0x3e1b28 still locked (state=40000001)</p>