为什么getInstrumentation()返回的上下文.getTargetContext()与getActivity()不同.getApplicationContext()?

时间:2011-04-04 22:20:07

标签: android unit-testing

getInstrumentation()。getTargetContext()返回一种android.app.Context类型。但我觉得它是android.app.ApplicationContext的基类,因为我在以下日志中得到了类强制转换异常。

getActivity()。getApplicationContext()返回一种android.app.Context类型,尽管它实际上返回了我的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.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

1 个答案:

答案 0 :(得分:-2)

See Question.我想我解决了自己的问题。