android getdecorview 出现空指针,android-Robolectric:构建失败,空指针异常

在Android Studio中开发使用Robolectric进行单元测试时遇到问题,测试失败并抛出NullPointerException。问题出在试图通过findViewById获取R.id.clickButton的Button对象时。解决方案是在调用Robolectric.buildActivity后添加.create()方法来初始化活动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我最近开始为我的android应用程序开发Robolectric单元测试框架.我正在为此使用Android Studio IDE.

我制作了MainActivity.java,其中有按钮R.id.clickButton,我能够在手机/仿真器中运行该应用程序,并且工作正常.

我制作了MainActivityTest.java,在这里我试图看到按钮不是null,如下所示

private MainActivity activity;

@Before

public void setup() {

activity = Robolectric.buildActivity(MainActivity.class).get();

}

@Test

public void shouldFail() {

assertTrue(true);

}

@Test

public void shouldNotBeNull() {

assertThat(activity).isNotNull();

Button button = (Button) activity.findViewById(R.id.clickButton);

assertThat(button).isNotNull();

}

当我使用命令行执行测试时,构建失败,原因为Button button =(Button)activity.findViewById(R.id.clickButton);错误跟踪.我找不到为什么.大家看着这个,希望您能尽快答复我.

堆栈跟踪

java.lang.NullPointerException

at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:132)

at android.view.ViewGroup.initViewGroup(ViewGroup.java:447)

at android.view.ViewGroup.__constructor__(ViewGroup.java:417)

at android.view.ViewGroup.(ViewGroup.java:416)

at android.widget.FrameLayout.(FrameLayout.java:93)

at org.robolectric.tester.android.view.RoboWindow$2.(RoboWindow.java:231)

at org.robolectric.tester.android.view.RoboWindow.createDecorView(RoboWindow.java:231)

at org.robolectric.tester.android.view.RoboWindow.access$000(RoboWindow.java:30)

at org.robolectric.tester.android.view.RoboWindow$1.run(RoboWindow.java:222)

at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:256)

at org.robolectric.tester.android.view.RoboWindow.getDecorView(RoboWindow.java:220)

at org.robolectric.tester.android.view.RoboWindow.findViewById(RoboWindow.java:292)

at org.robolectric.shadows.ShadowActivity.findViewById(ShadowActivity.java:312)

at android.app.Activity.findViewById(Activity.java)

at com.example.robolectrichelloworld.MainActivityTest.shouldNotBeNull(MainActivityTest.java:41)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)

at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)

at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:241)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)

at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:177)

at org.junit.runners.ParentRunner.run(ParentRunner.java:300)

at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)

at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)

at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)

at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:50)

at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)

at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)

at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)

at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)

at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)

at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:103)

at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)

at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)

at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:355)

at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

at java.lang.Thread.run(Thread.java:744)

解决方法:

尝试在Robolectric.buildActivity(…)之后插入对create()的调用:

@Before

public void setup() {

activity = Robolectric.buildActivity(MainActivity.class).create().get();

}

标签:robolectric,android

来源: https://codeday.me/bug/20191122/2056074.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值