Getting Instrumentation run failed due to 'java.lang.NullPointerException'

£可爱£侵袭症+ 提交于 2019-12-23 07:08:28

问题


I am trying to write Unit test cases for Activities in my app by extending the test class with ActivityUnitTestCase. I could successfully run the test cases earlier but now I'm always getting the exception while running them. Even though I'm pretty familiar with handling NullPointerExceptions, I couldn't figure out the problem that's causing this. I couldn't find any similar questions so I'm posting this one.

Stack trace shows me there is a null object reference at this line in my code

activity = startActivity(mIntent, null, null);

But the startActivity method is supposed to get the instance of the activity I'm testing. I'm not sure why it's returning null.

Here is the Stack trace.

java.lang.NullPointerException: Attempt to write to field 'android.os.IBinder android.app.ActivityThread.mLastIntendedActivityToken' on a null object reference
at android.app.Activity.performCreate(Activity.java:6372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.support.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:346)
at android.test.ActivityUnitTestCase.startActivity(ActivityUnitTestCase.java:158)
at com.abc.test.MainActivityTest.access$100(MainActivityTest.java:16)
at com.abc.test.MainActivityTest$1.run(MainActivityTest.java:34)
at android.app.Instrumentation$SyncRunnable.run(Instrumentation.java:1891)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6117)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

Test running failed: Instrumentation run failed due to 'java.lang.NullPointerException'

Here is the Test class

public class MainActivityTest extends ActivityUnitTestCase<MainActivity>{

    private Intent mIntent;
    private MainActivity activity;

    public MainActivityTest() {
        super(MainActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        //Create an intent to launch target Activity as it is not automatically started by Android Instrumentation
        mIntent = new Intent(getInstrumentation().getContext(), MainActivity.class);
        //Start the activity under test in isolation, in the main thread to avoid assertion error.
        getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                activity = startActivity(mIntent, null, null);
            }
        });
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    /**
     * Tests the preconditions of this test fixture.
     */
    @SmallTest
    public void testPreconditions() {
        assertNotNull("MainActivity is null", getActivity());
    }

    @MediumTest
    public void testSecondActivityWasLaunchedWithIntent() {
        // Get the intent for the next started activity
        final Intent launchIntent = getStartedActivityIntent();
        //Verify the intent was not null.
        assertNotNull("Intent was null", launchIntent);
        //Verify that LaunchActivity was finished
        assertTrue(isFinishCalled());
    }
}

回答1:


@prudhvi I don't think I have a silver bullet, unfortunately, but I would suggest trying to follow these steps to upgrade to the new test support libraries in the newer versions of the SDK. Sorry that I can't be of more help!




回答2:


I had the same issue. The solution was to make the test an ActivityInstrumentationTestCase2 instead of an ActivityUnitTestCase, and have the activity created for me in the background




回答3:


There's a good chance something in your application class (i.e. X extends Application) is crashing early on. If that's the case you'll see this error.... Checkout your logcat for traces.




回答4:


i changed ActivityUnitTestCase to ActivityInstrumentationTestCase2 and removed my startActivity() calls (seems that ActivityInstrumentationTestCase2 automatically starts the activity) now it runs again



来源:https://stackoverflow.com/questions/31926107/getting-instrumentation-run-failed-due-to-java-lang-nullpointerexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!