我有一个自定义主题的视图已解释here并且解决了所描述的问题,但当我在visible()
上调用ActivityController
时会导致以下错误。
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
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...
请注意,路径指向右侧res目录。
不调用visible()
我没有收到任何错误但是我需要测试一些在onCreateOptionsMenu()
内初始化的menuItem,而visible()
内部会调用TestMenu
{I}我已经尝试通过自己传递一个{{1}}对象来调用该方法,但它不起作用(并且也被弃用))
我正在使用robolectric 2.2,但我也尝试过2.3-SNAPSHOT
我真的很感激任何帮助,因为我的想法已经用完了。
答案 0 :(得分:8)
问题是因为您使用支持库并且robolectric与它不兼容。 ActionBarSherlock也是如此。
幸运的是,这个问题已经解决了。这对我有用。
根据sneuberger-amazon的帖子https://github.com/robolectric/robolectric/issues/:
问题是Robolectric提供了ShadowMenuInflater,但它没有提供ShadowSupportMenuInflater(这是什么 appcompat使用)。
你可以通过创建一个来解决这个问题 org.robolectric.shadows.ShadowSupportMenuInflater类,内容为:
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);
}
}
将此类放在您自己项目的org.robolectric.shadows包中。 这样Robolectric会在寻找阴影时找到它 SupportMenuInflater,以便您的所有测试都能自动获取 (无需为每个测试添加影子配置。)
答案 1 :(得分:1)
使用Robolectric 2.3,尝试使用
注释您的测试类@Config(qualifiers = "v10")