Robolectric 2.2 中的 android.content.res.Resources$NotFoundException

标签 android robolectric

我有一个带有自定义主题的 View ,如前所述here这解决了所描述的问题,但是当我在 ActivityController 上调用 visible() 时会导致以下错误。

android.content.res.Resources$NotFoundException: Resource ID #0x7f0c0000
at android.content.res.Resources.getValue(Resources.java:1118)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2304)
at android.content.res.Resources.getLayout(Resources.java:934)
at android.support.v7.internal.view.SupportMenuInflater.inflate(SupportMenuInflater.java:115)
at com.myCompany.myApp.activities.LogActivity.onCreateOptionsMenu(LogActivity.java:36)
at android.app.Activity.onCreatePanelMenu(Activity.java:2504)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)
at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:147)
at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:285)
at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:413)
at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:775)
at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:198)
at org.robolectric.util.Scheduler.postDelayed(Scheduler.java:37)
at org.robolectric.shadows.ShadowLooper.post(ShadowLooper.java:207)
at org.robolectric.shadows.ShadowHandler.postDelayed(ShadowHandler.java:56)
at android.os.Handler.postDelayed(Handler.java)
at android.view.ViewRootImpl$RunQueue.executeActions(ViewRootImpl.java:6230)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1239)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameHandler.handleMessage(Choreographer.java:664)
at org.robolectric.shadows.ShadowHandler.routeMessage(ShadowHandler.java:125)
at org.robolectric.shadows.ShadowHandler.access$100(ShadowHandler.java:25)
at org.robolectric.shadows.ShadowHandler$1.run(ShadowHandler.java:110)
at org.robolectric.util.Scheduler$PostedRunnable.run(Scheduler.java:162)
at org.robolectric.util.Scheduler.runOneTask(Scheduler.java:107)
at org.robolectric.util.Scheduler.advanceTo(Scheduler.java:92)
at org.robolectric.util.Scheduler.advanceToLastPostedRunnable(Scheduler.java:68)
at org.robolectric.util.Scheduler.unPause(Scheduler.java:25)
at org.robolectric.shadows.ShadowLooper.unPause(ShadowLooper.java:228)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:267)
at org.robolectric.util.ActivityController.visible(ActivityController.java:201)
at com.myCompany.myApp.test.activities.LogActivityTest.setUp(LogActivityTest.java:50)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:230)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:172)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

这是包含自定义主题的 syle.xml 文件

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

<style name="Theme.RobolectricCompatible" parent="@style/Theme.Base.AppCompat.Light.DarkActionBar">
    <item name="android:windowNoTitle">false</item>
</style>

这是我设置主题的 Activity block

<activity
        android:name=".activities.LogActivity"
        android:label="@string/log_activity_title"
        android:parentActivityName="com.myApp.activities.MainActivity"
        android:theme="@style/Theme.RobolectricCompatible" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
        android:value="com.myApp.activities.MainActivity" />
</activity>

我有另一个使用 Theme.Base.AppCompat.Light.DarkActionBar 主题的 Activity,我可以正确测试它,但如果我将主题更改为 Theme.RobolectricCompatible,它会显示相同的行为。

最后,这是我运行测试时 Eclipse 输出的内容:

    DEBUG: Loading resources for com.myCompany.myApp from C:\Users\luca\Android\myAppTest\..\myApp\res...
    DEBUG: Loading resources for android.support.v7.appcompat from C:\Users\luca\Android\myAppTest\..\myApp\..\android-support-v7-appcompat\res...
    DEBUG: Loading resources for android from jar:C:\Users\luca\.m2\repository\org\robolectric\android-all\4.3_r2-robolectric-0\android-all-4.3_r2-robolectric-0.jar!/res...

请注意路径指向正确的资源目录。

在不调用 visible() 的情况下,我没有收到任何错误,但我需要测试一些在 onCreateOptionsMenu() 中初始化的 menuItem ,而这又是在 visible() 中调用(我已经尝试通过自己传递 TestMenu 对象来调用该方法,但它不起作用(并且也已弃用))

我正在使用 robolectric 2.2,但我也尝试过使用 2.3-SNAPSHOT

我真的很感激任何帮助,因为我已经没有想法了。

最佳答案

问题是因为您使用了支持库,而 robolectric 与其不兼容。 ActionBarSherlock 也是如此。

幸运的是问题已经解决了。这对我有用。

根据 sneuberger-amazon 在 https://github.com/robolectric/robolectric/issues/ 上的帖子:

The problem is that Robolectric provides a ShadowMenuInflater, but it does not provide a ShadowSupportMenuInflater (which is what appcompat uses).

You can fix this by creating a org.robolectric.shadows.ShadowSupportMenuInflater class with contents:

package org.robolectric.shadows;

import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;

import android.support.v7.internal.view.SupportMenuInflater;
import android.view.Menu;

@Implements(SupportMenuInflater.class)
public class ShadowSupportMenuInflater extends ShadowMenuInflater {
    @Implementation
    public void inflate(int menuRes, Menu menu) {
        super.inflate(menuRes, menu);
    }
}

Put this class in your own project's org.robolectric.shadows package. That way Robolectric will find it when looking for a shadow for SupportMenuInflater, so that all your tests will get it automatically (no need to add a shadow Config to every test).

关于Robolectric 2.2 中的 android.content.res.Resources$NotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21264849/

相关文章:

android - 为什么此测试会触发 Robolectric Delegate runner RuntimeException?

android - Eclipse 的模拟器(ArcGIS Android 应用程序)?

android - 如何使用 robolectric 用额外的数据测试开始的 Intent

java - 如何在 Android 中使用 Dagger2 依赖注入(inject)和 Robolectric 进行测试?

Android - 如何在 listView 中移动滚动条

android - 在项目 'testDebug' 中找不到名称为 ':module' 的任务

android - 如何覆盖 Robolectric 运行时依赖库 URL?

java - 从 Uri 创建文件发生异常

android - android中的使用统计访问权限

android - OKHttp默认的重试策略机制是什么? (安卓)