android java.lang.nullpointerexception,android – 由于“java.lang.NullPointerException”导致Instrumentation...

我试图通过使用ActivityUnitTestCase扩展测试类,在我的应用程序中为活动编写单元测试用例.我可以早期成功运行测试用例,但现在我总是在运行它们时得到异常.即使我很熟悉处理NullPointerExceptions,但我无法弄清楚导致此问题.我找不到任何类似的问题,所以我发布这个.

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

activity = startActivity(mIntent,null,null);

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

@H_301_2@这是堆栈跟踪.

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'

@H_301_2@这是Test类

public class MainActivityTest extends ActivityUnitTestCase{

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());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值