提问人:Prudhvi 提问时间:8/11/2015 最后编辑:Prudhvi 更新时间:3/31/2016 访问量:3997
由于“java.lang.NullPointerException”,检测运行失败
Getting Instrumentation run failed due to 'java.lang.NullPointerException'
问:
我正在尝试通过使用 扩展测试类来为我的应用程序中的活动编写单元测试用例。我可以更早地成功运行测试用例,但现在我总是在运行它们时遇到异常。即使我非常熟悉处理,我也无法弄清楚导致这种情况的问题。我找不到任何类似的问题,所以我发布了这个问题。ActivityUnitTestCase
NullPointerExceptions
堆栈跟踪显示我的代码中这一行有一个空对象引用
activity = startActivity(mIntent, null, 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'
下面是 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, 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赞
Vito Andolini
8/11/2015
#1
@prudhvi不幸的是,我认为我没有灵丹妙药,但我建议尝试按照以下步骤升级到较新版本的 SDK 中的新测试支持库。对不起,我不能提供更多帮助!
0赞
Catalin
10/23/2015
#2
我有同样的问题。 解决方案是使测试成为 ActivityInstrumentationTestCase2 而不是 ActivityUnitTestCase,并在后台为我创建活动
0赞
steineron
11/26/2015
#3
您的应用程序类中的某些内容(即 X 扩展应用程序)很有可能在早期崩溃。如果是这种情况,您将看到此错误。检查您的 logcat 是否有痕迹。
0赞
wutzebaer
3/31/2016
#4
我将 ActivityUnitTestCase 更改为 ActivityInstrumentationTestCase2 并删除了我的 startActivity() 调用(似乎 ActivityInstrumentationTestCase2 会自动启动活动),现在它再次运行
评论