Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPE when calling context.createConfigurationContext(configuration) #2909

Closed
AndrazP opened this issue Feb 12, 2017 · 9 comments
Closed

NPE when calling context.createConfigurationContext(configuration) #2909

AndrazP opened this issue Feb 12, 2017 · 9 comments

Comments

@AndrazP
Copy link

AndrazP commented Feb 12, 2017

Description

Caused by: java.lang.NullPointerException
	at org.robolectric.shadows.ShadowConfiguration.setTo(ShadowConfiguration.java:33)
	at android.content.res.Configuration.setTo(Configuration.java)
	at org.robolectric.shadows.ShadowConfiguration.__constructor__(ShadowConfiguration.java:26)
	at android.content.res.Configuration.<init>(Configuration.java)
	at android.app.ResourcesManager.getTopLevelResources(ResourcesManager.java:229)
	at android.app.ContextImpl.__constructor__(ContextImpl.java:1838)
	at android.app.ContextImpl.<init>(ContextImpl.java)
	at android.app.ContextImpl.createConfigurationContext(ContextImpl.java:1713)
	at com.example.util.LocaleHelper.updateResources(LocaleHelper.java:121)
	at com.example.util.LocaleHelper.onAttach(LocaleHelper.java:113)
	at com.example.MyApplication.attachBaseContext(MyApplication.java:44)
	at android.app.Application.attach(Application.java:187)
	at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:231)
	at org.robolectric.util.ApplicationTestUtil.attach(ApplicationTestUtil.java:29)
	at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:134)
	at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:329)
	at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:259)
	at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:41)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:199)

Steps to Reproduce

Configuration configuration = new Configuration();
base.createConfigurationContext(configuration);

Robolectric & Android Version

Robolectric 3.2.2, Android 23

@jongerrish
Copy link
Contributor

I'm not sure what the issue is here. I added a test in the above PR and it passes. What configuration are you passing in. Can you share the code from MyApplication / LocaleHelper?

I've also removed ShadowConfiguration in the PR as I think we can just use real framework code, the shadow looked pretty incomplete and would have become out dated as new config options are added in newer SDKs, this might just magically fix your issue.

You can try this by creating your own custom shadow for config and making it have an empty class body with no @implements methods.

@AndrazP
Copy link
Author

AndrazP commented Feb 15, 2017

Hi @jongerrish,
I was able to reproduce the issue in a completely empty project just with calling

Configuration configuration = new Configuration();
base.createConfigurationContext(configuration);

You can check it here: https://github.com/AndrazP/npe-shadow-configuration

How can I override the current implementation of ShadowConfiguration?
I was not successful with @config(shadows={EmptyShadowConfiguration.class})

@jongerrish
Copy link
Contributor

@AndrazP Can you test with Robolectric 3.3 please. We've basically removed all the implentation of ShadowConfiguration so it should be using Framework code (we left it there to give users a migration path without breaking their builds).

I wrote a test to verify your behavior so it looks like it would be fixed:-

assertThat(RuntimeEnvironment.application.createConfigurationContext(new Configuration())).isNotNull();

Please reopen if this doesn't work on 3.3. for you.

@AndrazP
Copy link
Author

AndrazP commented Mar 4, 2017

It works. Thank you!

@martofeld
Copy link

martofeld commented Apr 25, 2017

@jongerrish Im still having this issue even after I updated to Robolectric 3.3.2 somehow the Configuration is still calling ShadowConfiguration.

Test configuration:
Robolectric 3.3,3.3.2
TestSdk 17,18,21

Bellow I post my stacktrace. Any ideas?

java.lang.NullPointerException
	at android.content.res.Configuration.setTo(Configuration.java:623)
	at org.robolectric.internal.bytecode.ProxyMaker$GeneratedProxy/329491727.setTo(Unknown Source)
	at org.robolectric.shadows.ShadowConfiguration.setTo(ShadowConfiguration.java:42)
	at android.content.res.Configuration.setTo(Configuration.java)
	at android.content.res.Configuration.__constructor__(Configuration.java:619)
	at android.content.res.Configuration.<init>(Configuration.java)
	at android.app.ResourcesManager.getTopLevelResources(ResourcesManager.java:219)
	at android.app.ContextImpl.__constructor__(ContextImpl.java:2261)
	at android.app.ContextImpl.<init>(ContextImpl.java)
	at android.app.ContextImpl.createConfigurationContext(ContextImpl.java:2149)
	at android.content.ContextWrapper.createConfigurationContext(ContextWrapper.java:682)
	at android.content.ContextWrapper.createConfigurationContext(ContextWrapper.java:682)
	at com.my.package.LocaleContextWrapper.wrap(LocaleContextWrapper.java:40)
	at com.my.package.LocaleContextWrapperTest.testWrap_shouldWrapOverCalligraphy(LocaleContextWrapperTest.java:45)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:488)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
	at org.junit.rules.RunRules.evaluate(RunRules.java:20)
	at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:209)
	at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:109)
	at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:36)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.robolectric.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:63)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:114)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:57)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:66)
	at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
	at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
	at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
	at com.sun.proxy.$Proxy3.processTestClass(Unknown Source)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:109)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
	at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:147)
	at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:129)
	at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404)
	at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
	at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:46)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)

@npike
Copy link

npike commented Jun 2, 2017

Seeing the same stacktrace unless I use @config(sdk = N)

@BroHammie
Copy link

9d2e978 by @brettchabot fixed my issue. Had to use the snapshot version https://github.com/robolectric/robolectric#using-snapshots

@tamaralu
Copy link

tamaralu commented Aug 22, 2018

@jongerrish,
I am getting this with robolectric 3.8 and the following code:
Configuration conf = context.getResources().getConfiguration();
conf = new Configuration(conf);
conf.setLocale(currLanguageLocale);
Context localizedContext = context.createConfigurationContext(conf);

@VKostenc
Copy link

VKostenc commented Nov 7, 2018

@jongerrish,
I am getting this with robolectric 3.8 and the following code:
Configuration conf = context.getResources().getConfiguration(); conf = new Configuration(conf); conf.setLocale(currLanguageLocale); Context localizedContext = context.createConfigurationContext(conf);

@tamaralu If your issue in receiving of localized string you simply have to annotate a method with @config(qualifiers="en").
#635 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants