调用getActivity之前访问来自TestSuite的安装程序中(应用程序上下文)()上下文、安装程序、应用程序、getActivity

2023-09-05 10:05:54 作者:逃不开他是身体扩散的癌

我有一个活动,从内拉从应用程序扩展类(应用程序上下文)的对象的OnCreate()方法。

I have an Activity that pulls an object from an Application extended class (application context) from within the OnCreate() method.

在单元测试这个活动,所需要的对象是不存在的,因为它是从previous活动人口,并存储在上述应用程序上下文。

When unit testing this activity, the object needed isn't there because it is populated from a previous Activity and stored in the above mentioned application context.

不用说,当我打电话 getActivity()从我的 ActivityInstrumentationTestCase2 扩展的测试情况下,我得到一个空指针异常。

Needless to say, when I call getActivity() from within my ActivityInstrumentationTestCase2 extended test case I get a null pointer exception.

我如何填充一个活动开始前的背景和有它可用于该活动

How can I populate the context before an activity is started and have it available to that Activity?

更新: 经过一番挖掘我的发现: this.getInstrumentation()getTargetContext(),然后将其转换为我的应用程序类型扩展的类。但我得到一个类转换异常和跟踪点,以这样的:

Updated: After a bit of digging I found: this.getInstrumentation().getTargetContext() and then cast it to the type of my Application extended class. But I get a class cast exception and the trace points to this:

04-04 21:02:27.036: INFO/TestRunner(431): started: testIt(edu.rockies.rockies.activity.courses.test.TopicTest)
04-04 21:02:27.126: INFO/TestRunner(431): failed: testIt(edu.rockies.rockies.activity.courses.test.TopicTest)
04-04 21:02:27.126: INFO/TestRunner(431): ----- begin exception -----
04-04 21:02:27.136: INFO/TestRunner(431): java.lang.ClassCastException: android.app.ApplicationContext
04-04 21:02:27.136: INFO/TestRunner(431):     at edu.rockies.rockies.activity.courses.test.TopicTest.setUp(TopicTest.java:27)
04-04 21:02:27.136: INFO/TestRunner(431):     at junit.framework.TestCase.runBare(TestCase.java:125)
04-04 21:02:27.136: INFO/TestRunner(431):     at junit.framework.TestResult$1.protect(TestResult.java:106)
04-04 21:02:27.136: INFO/TestRunner(431):     at junit.framework.TestResult.runProtected(TestResult.java:124)
04-04 21:02:27.136: INFO/TestRunner(431):     at junit.framework.TestResult.run(TestResult.java:109)
04-04 21:02:27.136: INFO/TestRunner(431):     at junit.framework.TestCase.run(TestCase.java:118)
04-04 21:02:27.136: INFO/TestRunner(431):     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
04-04 21:02:27.136: INFO/TestRunner(431):     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
04-04 21:02:27.136: INFO/TestRunner(431):     at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:430)
04-04 21:02:27.136: INFO/TestRunner(431):     at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
04-04 21:02:27.136: INFO/TestRunner(431): ----- end exception -----
04-04 21:02:27.156: INFO/TestRunner(431): finished: testIt(edu.rockies.rockies.activity.courses.test.TopicTest)

this.getInstrumentation()。getTargetContext()应该返回类型的上下文对象。但我得到的 android.app.ApplicationContext 类转换exeption这没有任何意义。

this.getInstrumentation().getTargetContext() is supposed to return an object of type context. But I get the android.app.ApplicationContext class cast exeption which doesn't make sense.

更新2:

我做了一些调查研究,发现本作android.app.Application

I did some more research and discovered this for android.app.Application

java.lang.Object
    android.content.Context
        android.app.ApplicationContext
            android.app.Application

不过,谷歌的自己的Andr​​oid的Javadoc指这个:

java.lang.Object
    android.content.Context
        android.content.ContextWrapper
            android.app.Application

这是怎么回事?不对了。

What's going on? Something's not right.

更新3:

我已经更换了code以下行:

I have replaced the following line of code:

this.getInstrumentation().getTargetContext();

这一行的code。

with this line of code.

this.getInstrumentation().getTargetContext().getApplicationContext();

虽然背景下解决得当,它似乎并不相同上下文中的活动的。

Although the context resolves properly, it doesn't seem to be the same context as the activity's.

推荐答案

好吧,这个问题解决了。要访问之前getActivity()被调用,你需要调用这个函数的上下文:

Ok, this issue is resolved. To get access to the context before getActivity() has been called you need to call this function:

Context context = this.getInstrumentation().getTargetContext().getApplicationContext();