我已查看了此错误的所有其他帖子和教程,但未找到解决方案。
我的应用程序有几个库。我试图在其中一个中运行测试,复制到根应用程序测试文件夹。无论我做什么,它都不会运行。我一直得到相同的空指针。这是令人沮丧的,因为我可以从库中运行它(作为一个独立的,而不是在这个新项目中)
运行配置已设置,我选择,类,选择此测试。我指定了测试运行器android.test.InstrumentationTestRunner
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/myprojectspath.missionst"
pkg: myprojectspath.missionst
Success
Running tests
Test running startedTest running failed: Instrumentation run failed due to 'java.lang.NullPointerException'
Empty test suite.
08-23 20:14:09.731 17785-17785/mypath
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: mypath.mobility, PID: 17785
java.lang.RuntimeException: Exception thrown in onCreate() of ComponentInfo{mypath.test/android.test.InstrumentationTestRunner}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.ClassLoader android.content.Context.getClassLoader()' on a null object reference
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5137)
at android.app.ActivityThread.access$1600(ActivityThread.java:179)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5972)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.ClassLoader android.content.Context.getClassLoader()' on a null object reference
at android.test.AndroidTestRunner.loadTestClass(AndroidTestRunner.java:87)
at android.test.AndroidTestRunner.setTestClassName(AndroidTestRunner.java:50)
at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:379)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5134)
at android.app.ActivityThread.access$1600(ActivityThread.java:179)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5972)
at java.lang.reflect.Method.invoke(Native Method)
at ja
va.lang.reflect.Method.invoke(Method.java:372)
我的构建文件
apply plugin: 'com.android.application'
android {
signingConfigs {
myConfig {
keyAlias '...'
storeFile file('...')
keyPassword 'android'
storePassword 'android'
}
}
compileSdkVersion 21
buildToolsVersion '21.1.2'
defaultConfig {
applicationId "projectpath"
minSdkVersion 19
targetSdkVersion 22
versionCode 1
versionName "1.0"
signingConfig signingConfigs.myConfig
testApplicationId "projectpath.missionst"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
signingConfig signingConfigs.myConfig
}
debug {
signingConfig signingConfigs.myConfig
}
}
lintOptions {
abortOnError false
}
productFlavors {
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':lib_a')
compile project(':lib_b')
compile 'com.google.android.gms:play-services:7.5.0'
compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
compile 'com.squareup.okhttp:okhttp:2.0.0'
compile 'com.github.alamkanak:android-week-view:1.2.3'
compile 'com.google.maps.android:android-maps-utils:0.3.4'
compile files('libs/rfc2445-4Mar2011.jar')
compile 'joda-time:joda-time:2.3'
compile files('libs/volley.jar')
compile 'com.google.code.gson:gson:2.3.1'
compile 'com.android.support:support-v13:22.2.1'
compile 'com.android.support:gridlayout-v7:22.2.1'
compile 'com.android.support:appcompat-v7:22.2.1'
}
测试文件 包路径;
import java.io.File;
import android.test.AndroidTestCase;
import lib_a_path.DataUtils;
public class UserNameTest extends AndroidTestCase {
private DataUtils du;
public UserNameTest() {
super();
}
protected void setUp() throws Exception {
super.setUp();
du = new DataUtils(getContext());
// It looks like the app is just reinstalled on the development environment
// and the files are not actually deleted.
// So we will manually delete the file in the setup phase
File[] initFile = getContext().getFilesDir().listFiles();
for (int i = 0; i < initFile.length; i++) {
initFile[i].delete();
}
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testSaveAndReadUserName() throws Exception {
File[] beforeFiles = getContext().getFilesDir().listFiles();
if (beforeFiles.length > 0) {
System.out.println("beforeFiles[0] = "+beforeFiles[0].getName());
}
assertEquals(beforeFiles.length, 0);
du.saveUserName("testuser");
File[] afterFiles = getContext().getFilesDir().listFiles();
assertEquals(afterFiles.length, 1);
assertEquals(afterFiles[0].getName(), "userName");
String realUser = du.getUserName();
assertEquals(realUser, "testuser");
}
}