开发者

Why is the returned context from getInstrumentation().getTargetContext() different than getActivity().getApplicationContext()?

getInstrumentation().getTargetContext() returns a type of android.app.Context. But I have a feeling that it is the base class for android.app.ApplicationContext because I am getting a Class cast exception in the following logs.

And

getActivity().getApplicationContext() returns a type of android.app.Context although it's really returning the base class of my activity defined by my implementation of ActivityInstrumentationTestCase2.

LogCat:

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开发者_JS百科.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)

Link


See Question. I think I solved my own issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜