title: Android instrumentation.callActivityOnPause causes "System services not available to Activities before onCreate()"
body: <p>I'm currently setting up unit test to test my camera app where I release the camera handle when the app goes into the background via the onPause callback. As soon as it gets up to the part where I call the onPause method of the activity I'd get:</p>
  
  <pre><code>java.lang.IllegalStateException: System services not available to Activities before onCreate()
  at android.app.Activity.getSystemService(Activity.java:4738)
  at android.app.Activity.getMultiWindowStyle(Activity.java:4563)
  at android.app.Activity.getMultiWindowStyle(Activity.java:4568)
  at android.app.Activity.onPause(Activity.java:1366)
  at com.ricky.chow.cameraopencv.activity.MainCameraActivity.onPause(MainCameraActivity.java:57)
  at android.app.Activity.performPause(Activity.java:5555)
  at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1239)
  at com.ricky.chow.cameraopencv.activity.MainCameraActivityTest.tearDownThisActivity(MainCameraActivityTest.java:58)
  at com.ricky.chow.cameraopencv.activity.MainCameraActivityTest.testCameraPreviewExists(MainCameraActivityTest.java:78)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
  at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
  at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
  at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
  at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
  at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1729)
  </code></pre>
  
  <p>Unit test:</p>
  
  <pre><code>public class MainCameraActivityTest extends ActivityUnitTestCase&lt;MainCameraActivity&gt; {
  
  private Intent mLaunchIntent;
  private ImageButton captureButton;
  private FrameLayout cameraPreviewView;
  private Instrumentation mInstrumentation;
  private MainCameraActivity mActivity;
  public MainCameraActivityTest(Class&lt;MainCameraActivity&gt; activityClass) {
      super(activityClass);
  }
  public MainCameraActivityTest() {
      super(MainCameraActivity.class);
  }
  
  
  private void setUpThisActivity()
  {
      mLaunchIntent = new Intent(getInstrumentation().getTargetContext(),
              MainCameraActivity.class);
      mInstrumentation = getInstrumentation();
      mActivity = startActivity(mLaunchIntent, null, null);
  
      mInstrumentation.callActivityOnStart(mActivity);
      mInstrumentation.callActivityOnResume(mActivity);
      captureButton = (ImageButton)mActivity.findViewById(R.id.CaptureButton);
      cameraPreviewView = (FrameLayout)mActivity.findViewById(R.id.CameraPreview);
  }
  
  private void tearDownThisActivity()
  {
      mInstrumentation.callActivityOnPause(mActivity);
      mInstrumentation.callActivityOnStop(mActivity);
  }
  
  public void testCameraPrieviewWorks()
  {
      setUpThisActivity();
      MedianCameraPost mMedianCam = (MedianCameraPost) cameraPreviewView.getChildAt(0);
      Assert.assertNotNull(mMedianCam);
      Assert.assertEquals(mMedianCam.getVisibility(), View.VISIBLE);
      tearDownThisActivity();
  }
  </code></pre>
  
  <p>}</p>