Toolkit error due to "android.content.res.Resources $ NotFoundException"

I am trying to run tests using espresso 2.2.1 in Android Studio 1.5.1. When I run LoginActivityTest, I get this error: "android.content.res.Resources $ NotFoundException", caused when LoginActivity calls MyService.java and MyService needs whole resources (i.e. Rinteger.number_of_days). These resources are defined in the R.integer.xml file in the gradle module (version 1.5.0).

Project Structure:

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

My test class:

 @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()); } } 

stacktrace error:

 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.insoft.android.lib.auth.infrastructure.AuthenticatorPreferences.<init>(MyService.java:36) at it.insoft.android.lib.auth.infrastructure.AuthenticatorPreferences.getInstance(MyService.java:45) at it.insoft.android.lib.auth.application.BaseLoginActivity.onCreate(BaseLoginActivity.java:27) at it.insoft.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.insoft.android.novae/it.insoft.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.insoft.android.lib.auth.infrastructure.AuthenticatorPreferences.<init>(AuthenticatorPreferences.java:36) at it.insoft.android.lib.auth.infrastructure.AuthenticatorPreferences.getInstance(MyService.java:45) at it.insoft.android.lib.auth.application.BaseLoginActivity.onCreate(BaseLoginActivity.java:27) at it.insoft.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' 

How can I fix this problem?

+6
source share
2 answers

If you use JUnit4 in your test and ActivityTestRule , you do not need to extend ActivityInstrumentationTestCase2<LoginActivity> and you do not need a constructor. Correct your code so that it looks like this and try again:

 @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()); } } 

UPDATE: see comment in code

+1
source

I had the same problem, problem in build.gradle

I lower 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' 
0
source

All Articles