Android 由于';java.lang.NullPointerException';

Android 由于';java.lang.NullPointerException';,android,nullpointerexception,android-instrumentation,activityunittestcase,Android,Nullpointerexception,Android Instrumentation,Activityunittestcase,我试图通过使用ActivityUnitTestCase扩展测试类,为我的应用程序中的活动编写单元测试用例。我可以早些时候成功地运行测试用例,但现在我总是在运行它们时遇到异常。尽管我对处理NullPointerExceptions非常熟悉,但我无法找出导致此问题的原因。我找不到任何类似的问题,所以我发布了这个 堆栈跟踪显示代码中这一行有一个空对象引用 activity = startActivity(mIntent, null, null); 但是startActivity方法应该得到我正在测试

我试图通过使用
ActivityUnitTestCase
扩展测试类,为我的应用程序中的活动编写单元测试用例。我可以早些时候成功地运行测试用例,但现在我总是在运行它们时遇到异常。尽管我对处理
NullPointerExceptions
非常熟悉,但我无法找出导致此问题的原因。我找不到任何类似的问题,所以我发布了这个

堆栈跟踪显示代码中这一行有一个空对象引用

activity = startActivity(mIntent, null, null);
但是
startActivity
方法应该得到我正在测试的活动的实例。我不知道它为什么返回空值

这是堆栈跟踪

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'
这是考试班

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());
    }
}
public类MainActivityTest扩展了ActivityUnitTestCase{
私人意图;
私人活动;
公共维护活动测试(){
超级(MainActivity.class);
}
@凌驾
受保护的void setUp()引发异常{
super.setUp();
//创建启动目标活动的意图,因为它不是由Android Instrumentation自动启动的
minent=newintent(getInstrumentation().getContext(),MainActivity.class);
//在主线程中单独启动被测活动,以避免断言错误。
getInstrumentation().runOnMainSync(新的Runnable()命令){
@凌驾
公开募捐{
activity=startActivity(minent,null,null);
}
});
}
@凌驾
受保护的void tearDown()引发异常{
super.tearDown();
}
/**
*测试此测试夹具的先决条件。
*/
@小测验
public void testprepositions(){
assertNotNull(“MainActivity为null”,getActivity());
}
@中考
public void TestSecondActivity已在内容()中启动{
//获取下一个已启动活动的意图
最终意图启动意图=getStartedActivityIntent();
//验证意图是否为空。
assertNotNull(“意图为空”,launchIntent);
//验证启动活动是否已完成
assertTrue(isFinishCalled());
}
}

@prudhvi不幸的是,我认为我没有灵丹妙药,但我建议尝试按照此方法升级到新版本SDK中的新测试支持库。对不起,我帮不上什么忙了

我也有同样的问题。
解决方案是将测试设置为ActivityInstrumentationTestCase2而不是ActivityUnitTestCase,并在后台为我创建活动

您的应用程序类(即X extends应用程序)中很有可能在早期崩溃。如果是这种情况,您将看到此错误。。。。检查日志猫以查看跟踪。

我将ActivityUnitTestCase更改为ActivityInstrumentationTestCase2,并删除了我的startActivity()调用(ActivityInstrumentationTestCase2似乎自动启动活动),现在它再次运行

@Marcin这不仅仅是重复。我尝试过调试,但没有任何线索,也找不到任何相关问题。我同意,这不是一个简单的NullPointerException案例。该异常是从Android的ActivityUnitTestCase类的调用链中引发的。@prudnvi我在编写的一个类似的测试用例中得到了相同的异常。我在一个棒棒糖装置上进行测试。我刚刚在KitKat上运行了测试,测试运行成功。你在用什么样的试跑器?我正在使用Google InstrumentationTestRunner。我想知道我们是否需要升级到新的测试运行程序:AndroidJUnitRunner,它在SDK的较新版本中。@Marcin您能将其取消标记为副本吗?“我想肯定不是。”VitoAndolini我可以在Nexus_5_API_22_x86 emulator(棒棒糖)中成功运行这些测试用例,无论是否使用AndroidJUnitRunner。但测试用例在Nexus_4_API_19(Kitkat)仿真器和三星S6设备上失败。(不过,它们都抛出了不同的异常)。S6正在投掷NPE。