robolectric/robolectric — pull request #895 by hborders (2014-01-03 22:18:41+00:00)
Title: Calling Looper.getMainLooper() in an Application's constructor throws NullPointerException

`ShadowLooper.getMainLooper()` throws a `NullPointerException` if called before `Robolectric.application` is set. This can happen if you call `Looper.getMainLooper()` in your `Application` subclass' constructor, like so:

public class MyApplication extends Application {
private final Handler handler = new Handler(Looper.getMainLooper());
}

This works fine on device, but it fails in Robolectric.

I'm new here, so I elected for a change that was least likely to break. It would probably be to save the main `Looper` in a `static` `SoftReference` so that consumers could always call `getMainLooper()` regardless of the `Thread` they're on. However, this would require changing `ShadowLooper.resetThreadLoopers()`, and as I said, I wanted to make this change as small as possible.