NPE when creating activity based on MapActivity #972

ryazmin opened this Issue Feb 18, 2014 · 8 comments

4 participants


Activity is very simple without map view in layout, it's just extends

the test code

public void launchActivity() throws Exception {

Issue looks similar to #918
and similar question on SO:

Exception stack trace:
at org.fest.reflect.method.Invoker.invoke(
at org.robolectric.util.ActivityController.attach(
at org.robolectric.util.ActivityController$
at org.robolectric.shadows.ShadowLooper.runPaused(
at org.robolectric.util.ActivityController.create(
at org.robolectric.util.ActivityController.create(
at com.mytests.activity.SimpleActivityTest.launchActivity(
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(
at org.junit.runners.model.FrameworkMethod.invokeExplosively(
at org.junit.internal.runners.statements.InvokeMethod.evaluate(
at org.junit.internal.runners.statements.RunBefores.evaluate(
at org.robolectric.RobolectricTestRunner$2.evaluate(
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(
at org.junit.runners.BlockJUnit4ClassRunner.runChild(
at org.junit.runners.BlockJUnit4ClassRunner.runChild(
at org.junit.runners.ParentRunner$
at org.junit.runners.ParentRunner$1.schedule(
at org.junit.runners.ParentRunner.runChildren(
at org.junit.runners.ParentRunner.access$000(
at org.junit.runners.ParentRunner$2.evaluate(
at org.robolectric.RobolectricTestRunner$1.evaluate(
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(


any workaround welcome


Have you tried debugging to know what is null in the activity oncreate?


I tried to debug and as I can see the member mFragments of is null for some reason.
Definition of this member:
final FragmentManagerImpl mFragments = new FragmentManagerImpl();

Here is the source of 'attach' method and crash happens right after attachBaseContext returns.

final void attach(Context context, ActivityThread aThread,
        Instrumentation instr, IBinder token, int ident,
        Application application, Intent intent, ActivityInfo info,
        CharSequence title, Activity parent, String id,
        NonConfigurationInstances lastNonConfigurationInstances,
        Configuration config) {

    mFragments.attachActivity(this, mContainer, null);


activity instance created by ActivityController:
public ActivityController(Class activityClass) {
this.activity = constructor().in(activityClass).newInstance();
shadowActivity = shadowOf_(activity);
shadowMainLooper = shadowOf_(Looper.getMainLooper());

at this moment mFragments member already null

then ActivityController.create called and controller tries to attach activity
and basically when Activity.attach is invoked crash happens.


Weird indeed.

I'm assuming this works on a real device. Also, mFragments must probably is a private or package local field that you should not possibly be able to set to null.

are you using a support library for your fragments?

I really have no idea what may be causing this.


More debugging and as I can see the problem in ShadowMapActivity.
The initial issue (NPE) was fixed by changing the constructor method in ShadowMapActivity
(currently it's defined but does nothing, empty)

Changed to:
public void constructor() {

Now activity was successfully created, but then I got SuperNotCalledException for onResume.
ShadowMapActivity implements onResume:

public void onResume() {
registerReceiver(connectivityBroadcastReceiver, new IntentFilter());

I tried to remove onResume implementation from ShadowMapActivity, but got
java.lang.RuntimeException: stub
at Source)

So, I just changed it to:

public void onResume() {
registerReceiver(connectivityBroadcastReceiver, new IntentFilter());

and seems now everything works, well, at-least for current simple test I used for debugging.


so, as workaround, I created custom shadow from ShadowMapActivity with changes above,
and added it to as
shadows: com.mytests.shadows.ShadowMapActivityWorkaround


@ryazmin Do you still need that Shadow with Robolectric 3.0?

If so, submit a PR.

Robolectric member
erd commented Jul 2, 2015

Closing this since it seems like there is a workaround.

@erd erd closed this Jul 2, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment