我在极早的 android 加载阶段不断收到 Nullpointer 异常:
Launching instrumentation android.test.InstrumentationTestRunner on 3031C2FF986F00EC
[2013-01-13 02:53:30 - Login] Test run failed: Instrumentation run failed due to 'java.lang.NullPointerException'
这是因为我插入了一些触发此错误的 OrmLiteSqliteOpenHelper(我猜也可能是 SqliteOpenHelper)子类方法中的一段代码。似乎这个代码在加载过程中被调用得太早,以至于发生了这种情况。不幸的是,程序在加载过程的早期就没有断点停止。
我已经在其他代码位置广泛并成功地测试了代码。
那么我怎么知道什么时候所有的东西都加载到了android中,我可以在没有错误的情况下运行这段代码呢?
仅供参考:它是一段从服务器获取数据库内容并将其保存到本地 sqllite 数据库的代码,以便在我启动程序时数据库始终处于相同的状态(由服务器决定)。
好的,我会尝试编辑并发布一些代码
public class DatabaseTests extends ActivityInstrumentationTestCase2<UebersichtEinsaetze> {
private DatabaseHelperBase helper;
private UebersichtEinsaetze mainActivity;
public DatabaseTests() {
super("de.innosoft.android", UebersichtEinsaetze.class);
}
// throws exception -.-
protected void setUp() throws Exception {
super.setUp();
它与单元测试文件一起停在那里。不知道它从那里去哪里,但我知道它到达了(因为它在 UebersichtEinsaetze 的某个地方调用了该函数,并且超出了它与 SimpleCursorFactory 一起使用的地方):
public <T extends Entity<T, I>, I> RuntimeExceptionDao<T, I> getRTE(Class<T> clazz) {
RuntimeExceptionDao<T, I> rte = super.getRTE(clazz);
initTestdata();
return rte;
}
initTestdata 是:
if (!init)
{
new HttpBase(new HttpGet(), "setup", "InitTestdata").evaluate();
if (!isTestdataUpToData())
{
this.dropTables();
this.createTables();
try {
Map<Class<?>, List<Object>> m = ControllerMethod.GETALL.testServer();
for (EntityRef<?> entityRef : Entity.entities) {
List<Object> l = m.get(entityRef.getClazz());
if (entityRef.getSizeOfTestTable() != l.size())
throw new Exception("Testdata inconsistency");
for (Object entity : l) {
Object dao = this.getClass().getMethod("getRTE", Class.class).invoke(this, entityRef.getClazz());
dao.getClass().getMethod("create", Object.class).invoke(dao, entity);
}
}
} catch (Exception e) {
ExceptionHandler.handle(e);
}
}
init = true;
}
}
这就是发生异常的地方。如果我删除此代码块并将某个目录粘贴到单元测试中(例如),它就可以工作。但我不想那样做。
日志猫:
01-13 03:32:18.594: I/dalvikvm(24888): Turning on JNI app bug workarounds for target SDK version 8...
01-13 03:32:18.707: D/dalvikvm(24888): GC_CONCURRENT freed 146K, 13% free 2657K/3044K, paused 3ms+8ms, total 33ms
01-13 03:32:18.765: I/ActivityThread(24888): Pub de.innosoft.android: de.innosoft.android.test
01-13 03:32:18.816: D/dalvikvm(24888): GC_CONCURRENT freed 164K, 13% free 2983K/3396K, paused 3ms+3ms, total 37ms
01-13 03:32:18.886: D/dalvikvm(24888): GC_CONCURRENT freed 228K, 14% free 3222K/3720K, paused 3ms+2ms, total 23ms
01-13 03:32:18.886: D/dalvikvm(24888): WAIT_FOR_CONCURRENT_GC blocked 13ms
01-13 03:32:18.914: D/dalvikvm(24888): GC_FOR_ALLOC freed 245K, 16% free 3317K/3912K, paused 16ms, total 16ms
01-13 03:32:18.937: D/dalvikvm(24888): GC_FOR_ALLOC freed 241K, 15% free 3411K/4008K, paused 16ms, total 16ms
01-13 03:32:19.011: D/dalvikvm(24888): GC_CONCURRENT freed 241K, 12% free 3631K/4112K, paused 2ms+3ms, total 38ms
01-13 03:32:19.140: D/dalvikvm(24888): GC_CONCURRENT freed 96K, 8% free 4031K/4340K, paused 3ms+3ms, total 49ms
01-13 03:32:19.265: D/dalvikvm(24888): GC_CONCURRENT freed 324K, 12% free 4259K/4824K, paused 2ms+4ms, total 51ms
01-13 03:32:19.422: D/dalvikvm(24888): GC_CONCURRENT freed 505K, 15% free 4387K/5132K, paused 2ms+4ms, total 55ms
01-13 03:32:19.484: W/dalvikvm(24888): Exception Ljava/lang/NullPointerException; thrown while initializing Lde/innosoft/android/data/Consts;
01-13 03:32:19.484: D/AndroidRuntime(24888): Shutting down VM
01-13 03:32:19.484: W/dalvikvm(24888): threadid=1: thread exiting with uncaught exception (group=0x40db5930)
01-13 03:32:19.492: E/AndroidRuntime(24888): FATAL EXCEPTION: main
01-13 03:32:19.492: E/AndroidRuntime(24888): java.lang.ExceptionInInitializerError
01-13 03:32:19.492: E/AndroidRuntime(24888): at de.innosoft.android.data.HttpBase.<init>(HttpBase.java:76)
01-13 03:32:19.492: E/AndroidRuntime(24888): at de.innosoft.android.data.DatabaseHelperTestdata.initTestdata(DatabaseHelperTestdata.java:46)
01-13 03:32:19.492: E/AndroidRuntime(24888): at de.innosoft.android.data.DatabaseHelperTestdata.getRTE(DatabaseHelperTestdata.java:75)
01-13 03:32:19.492: E/AndroidRuntime(24888): at de.innosoft.android.test.onCreate(test.java:66)
01-13 03:32:19.492: E/AndroidRuntime(24888): at android.content.ContentProvider.attachInfo(ContentProvider.java:1058)
01-13 03:32:19.492: E/AndroidRuntime(24888): at android.app.ActivityThread.installProvider(ActivityThread.java:4969)
01-13 03:32:19.492: E/AndroidRuntime(24888): at android.app.ActivityThread.installContentProviders(ActivityThread.java:4582)
01-13 03:32:19.492: E/AndroidRuntime(24888): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4524)
01-13 03:32:19.492: E/AndroidRuntime(24888): at android.app.ActivityThread.access$1300(ActivityThread.java:150)
01-13 03:32:19.492: E/AndroidRuntime(24888): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
01-13 03:32:19.492: E/AndroidRuntime(24888): at android.os.Handler.dispatchMessage(Handler.java:99)
01-13 03:32:19.492: E/AndroidRuntime(24888): at android.os.Looper.loop(Looper.java:137)
01-13 03:32:19.492: E/AndroidRuntime(24888): at android.app.ActivityThread.main(ActivityThread.java:5191)
01-13 03:32:19.492: E/AndroidRuntime(24888): at java.lang.reflect.Method.invokeNative(Native Method)
01-13 03:32:19.492: E/AndroidRuntime(24888): at java.lang.reflect.Method.invoke(Method.java:511)
01-13 03:32:19.492: E/AndroidRuntime(24888): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
01-13 03:32:19.492: E/AndroidRuntime(24888): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
01-13 03:32:19.492: E/AndroidRuntime(24888): at dalvik.system.NativeStart.main(Native Method)
01-13 03:32:19.492: E/AndroidRuntime(24888): Caused by: java.lang.NullPointerException
01-13 03:32:19.492: E/AndroidRuntime(24888): at de.innosoft.android.data.Consts.<clinit>(Consts.java:36)
01-13 03:32:19.492: E/AndroidRuntime(24888): ... 18 more