由于“android.content.res.Resources $NotFoundException”,仪器运行失败

8

我尝试在Android Studio 1.5.1中使用espresso 2.2.1运行测试。当我运行LoginActivityTest时,我遇到了这个错误:"android.content.res.Resources$NotFoundException",这是由于LoginActivity调用MyService.java并且MyService需要一个整数资源(即R.integer.number_of_days)。这个资源在gradle(版本1.5.0)模块的R.integer.xml文件中定义。

项目结构:

RootFolder/ ├----projectA/ │ ├----build.gradle │ ├----settings.gradle │ └----src/androidTest/java/.../LoginActivityTest │ └----src/main/java/.../LoginActivity │ └----Module/ ├----krill/ │ └----build.gradle │ ├----settings.gradle │ └----src/main/ | └----java/service/MyService.java | └----res/value/integers.xml │ └----otherModule/ └----build.gradle

我的测试类:

@RunWith(AndroidJUnit4.class)
@LargeTest
public class LoginActivityTest extends ActivityInstrumentationTestCase2<LoginActivity >{

@Rule
public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule(LoginActivity.class);

public LoginActivityTest() {
    super(LoginActivity.class);
}

@Test
public void testConfigDialog() {
    onView(withId(R.layout.login_custom_view));

    onView(withId(R.id.id_username)).perform(clearText());
    onView(withId(R.id.id_password)).perform(clearText());
}
}

错误堆栈:

Running tests
Test running started
android.content.res.Resources$NotFoundException: Resource ID #0x7f090004
at android.content.res.Resources.getValue(Resources.java:1233)
at android.content.res.Resources.getInteger(Resources.java:989)
at it.company.android.lib.auth.infrastructure.AuthenticatorPreferences.<init>(MyService.java:36)
at it.company.android.lib.auth.infrastructure.AuthenticatorPreferences.getInstance(MyService.java:45)
at it.company.android.lib.auth.application.BaseLoginActivity.onCreate(BaseLoginActivity.java:27)
at it.company.android.novae.application.LoginActivity.onCreate(LoginActivity.java:57)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.support.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:534)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)


java.lang.RuntimeException: Unable to start activity ComponentInfo{it.company.android.novae/it.company.android.appname.application.LoginActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f090004
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f090004
at android.content.res.Resources.getValue(Resources.java:1233)
at android.content.res.Resources.getInteger(Resources.java:989)
at it.company.android.lib.auth.infrastructure.AuthenticatorPreferences.<init>(AuthenticatorPreferences.java:36)
at it.company.android.lib.auth.infrastructure.AuthenticatorPreferences.getInstance(MyService.java:45)
at it.company.android.lib.auth.application.BaseLoginActivity.onCreate(BaseLoginActivity.java:27)
at it.company.android.novae.application.LoginActivity.onCreate(LoginActivity.java:57)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.support.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:534)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
... 10 more

Test running failed: Instrumentation run failed due to 'android.content.res.Resources$NotFoundException'

我该如何解决这个问题?

如果您手动执行相同的操作,它是否正常工作? - Sufian
如果我运行应用程序(不进行测试),一切都正常。我不确定我是否回答了你的问题。 - Insoft
更新到classpath 'com.android.tools.build:gradle:2.0.0-beta3'后,我仍然遇到相同的错误。看起来谷歌刚刚发布了它。昨天我没有这个问题:( - Hesam
@Insoft 我在尝试进行仪器测试时遇到了与你类似的错误,因为我的项目包含一个模块。你解决了如何运行测试的问题吗? - Matthew Carlson
@Insoft 我找到了问题所在:如果我有androidTestCompile project(':LibraryModule'),那么R.java文件将无法正确生成。当我移除它时,问题得以解决。我仍然可以在我的测试中使用该库。 - Matthew Carlson
R.layout.view?不对,下面denys提到了是R.id.view。 - Saik Caskey
2个回答

1
如果您在测试中使用JUnit4和ActivityTestRule,则不需要扩展ActivityInstrumentationTestCase2<LoginActivity>并且不需要构造函数。修复您的代码使其看起来像这样,然后再试一次:
@RunWith(AndroidJUnit4.class)
@LargeTest
public class LoginActivityTest {

    @Rule
    public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule(LoginActivity.class);

    @Test
    public void testConfigDialog() {
        onView(withId(R.layout.login_custom_view)); //you can't use layout id here there must be a view id

        onView(withId(R.id.id_username)).perform(clearText());
        onView(withId(R.id.id_password)).perform(clearText());
    }
}

更新:请看代码中的注释。

看一下我的更新,你不能在 onView() 中使用布局 ID。此外,它没有任何像点击或检查的操作。移除那行代码。 - denys
好的,我已经删除了那一行代码,但是我仍然得到相同的错误。我认为这是因为ActivityTestRule初始化LoginActivity时调用了MyService。所以问题出现在运行testConfigDialog方法之前。 - Insoft
我认为问题在于,当我启动测试时,上下文没有获取(或者是不可见的)正确的资源文件,或者类似的情况。这可能是原因吗? - Insoft

0

我遇到了同样的问题,问题出在 build.gradle 文件中。

我将 Espresso 降级了。

androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接