I just set up my first JUnit Test Case Project for the Android application I'm developing. I've added a (very simple) Test Case for one of the projects classes, but can't seem to get it to run. I've followed a couple of separate tutorials just to be sure I wasn't overlooking something when writing the test case.
I'm getting a NullPointerException - Attempt to invoke virtual method 'java.lang.Classloader android.content.Context.getClassLoader() on a null object reference
. I've done multiple searches looking for the same issue, but haven't been able to find a solution. Below is the logcat output (note: I've replaced my package names with generic terms before posting):
11-10 09:56:42.942: E/AndroidRuntime(31147): FATAL EXCEPTION: main
11-10 09:56:42.942: E/AndroidRuntime(31147): Process: <target package name>, PID: 31147
11-10 09:56:42.942: E/AndroidRuntime(31147): java.lang.RuntimeException: Exception thrown in onCreate() of ComponentInfo{<test package name>/android.test.InstrumentationTestRunner}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.ClassLoader android.content.Context.getClassLoader()' on a null object reference
11-10 09:56:42.942: E/AndroidRuntime(31147): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5107)
11-10 09:56:42.942: E/AndroidRuntime(31147): at android.app.ActivityThread.access$1600(ActivityThread.java:177)
11-10 09:56:42.942: E/AndroidRuntime(31147): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1509)
11-10 09:56:42.942: E/AndroidRuntime(31147): at android.os.Handler.dispatchMessage(Handler.java:102)
11-10 09:56:42.942: E/AndroidRuntime(31147): at android.os.Looper.loop(Looper.java:145)
11-10 09:56:42.942: E/AndroidRuntime(31147): at android.app.ActivityThread.main(ActivityThread.java:5942)
11-10 09:56:42.942: E/AndroidRuntime(31147): at java.lang.reflect.Method.invoke(Native Method)
11-10 09:56:42.942: E/AndroidRuntime(31147): at java.lang.reflect.Method.invoke(Method.java:372)
11-10 09:56:42.942: E/AndroidRuntime(31147): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
11-10 09:56:42.942: E/AndroidRuntime(31147): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
11-10 09:56:42.942: E/AndroidRuntime(31147): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.ClassLoader android.content.Context.getClassLoader()' on a null object reference
11-10 09:56:42.942: E/AndroidRuntime(31147): at android.test.AndroidTestRunner.loadTestClass(AndroidTestRunner.java:87)
11-10 09:56:42.942: E/AndroidRuntime(31147): at android.test.AndroidTestRunner.setTestClassName(AndroidTestRunner.java:50)
11-10 09:56:42.942: E/AndroidRuntime(31147): at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:379)
11-10 09:56:42.942: E/AndroidRuntime(31147): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5104)
11-10 09:56:42.942: E/AndroidRuntime(31147): ... 9 more
That sounds config/setup related (assuming you're using Android Studio here):
As you're getting a NPE on InstrumentationTestRunner, I reckon you've got Unit Test selected and that's causing the problem.
It's described in this section of the Vogella tutorial on testing in more detail.
Turns out (as suspected) I overlooked something fairly obvious. I am actually extending an Instrumentation Test class, and though I had an Android device connected, I did not have the activity to be tested running.
Ran my app first, then the test case, and it worked fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.