gpt4 book ai didi

android - 由于 'android.content.res.Resources$NotFoundException',检测运行失败

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:01:22 25 4
gpt4 key购买 nike

我尝试在 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 文件中定义。

项目结构:

根文件夹/
├----项目A/
│ ├----build.gradle
│ ├----settings.gradle
│ └----src/androidTest/java/.../LoginActivityTest
│ └----src/main/java/.../LoginActivity

└----模块/
├----磷虾/
│ └----build.gradle
│ ├----settings.gradle
│ └----源/主/
| └----java/服务/MyService.java
| └----res/value/integers.xml

└----其他模块/
└----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'

我该如何解决这个问题?

最佳答案

如果您在测试中使用 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());
}
}

更新:查看代码中的注释

关于android - 由于 'android.content.res.Resources$NotFoundException',检测运行失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35342065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com