android – 由于“java.lang.NullPointerException”导致Instrumentation运行失败

android – 由于“java.lang.NullPointerException”导致Instrumentation运行失败,第1张

概述我试图通过使用ActivityUnitTestCase扩展测试类,在我的应用程序中为活动编写单元测试用例.我可以早期成功运行测试用例,但现在我总是在运行它们时得到异常.即使我很熟悉处理NullPointerExceptions,但我无法弄清楚导致此问题.我找不到任何类似的问题,所以我发布这个. 堆栈跟踪显示我的代码中的这一行有一个空对象引用 activity = startActivity(mIn 我试图通过使用ActivityUnitTestCase扩展测试类,在我的应用程序中为活动编写单元测试用例.我可以早期成功运行测试用例,但现在我总是在运行它们时得到异常.即使我很熟悉处理NullPointerExceptions,但我无法弄清楚导致此问题.我找不到任何类似的问题,所以我发布这个.

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

activity = startActivity(mIntent,null,null);

但是StartActivity方法应该是获取我正在测试的活动的实例.我不知道为什么它返回null.

这是堆栈跟踪.

java.lang.NullPointerException: Attempt to write to fIEld 'androID.os.IBinder androID.app.ActivityThread.mLastIntendedActivityToken' on a null object referenceat 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.access0(MainActivityTest.java:16)at com.abc.test.MainActivityTest.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'

这是Test类

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);            }        });    }    @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());    }}
解决方法 @prudhvi我不认为我有一个银d,不幸的是,但我建议尝试跟随 these steps升级到SDK的较新版本的新的测试支持库.对不起,我不能更多的帮助! 总结

以上是内存溢出为你收集整理的android – 由于“java.lang.NullPointerException”导致Instrumentation运行失败全部内容,希望文章能够帮你解决android – 由于“java.lang.NullPointerException”导致Instrumentation运行失败所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1131964.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存