我从 Google 收到无法重现的随机 NullPointerException 崩溃报告。我正在使用对扩展 OrmLiteSqliteOpenHelper 的类的静态引用。助手在扩展 SherlockFragmentActivity 的主类中初始化。
if (DatabaseHelper.getInstance() == null) {
DatabaseHelper.setInstance(this.getApplicationContext());
}
起初我虽然可能会在设备旋转或发送到后台时发生。但是没有多少测试能够重现这个问题。
代码中没有任何地方将静态 mHelper 变量设置为 null
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
…
private static DatabaseHelper mHelper;
…
public static DatabaseHelper getInstance() {
return DatabaseHelper.mHelper;
}
public static void setInstance(Context context) {
DatabaseHelper.mHelper = new DatabaseHelper(context);
}
…
}
有谁知道可能是什么原因造成的?
以下是其中一份报告:
java.lang.RuntimeException: Unable to start activity ComponentInfo{au.com.dcbs.tradies/au.com.dcbs.tradies.presentation.document.DocumentActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2049)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2083)
at android.app.ActivityThread.access$600(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1233)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4697)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at bd.a(Document.java:654)
at au.com.dcbs.tradies.presentation.document.DocumentActivity$a.b(DocumentActivity.java:102)
at au.com.dcbs.tradies.presentation.document.DocumentActivity$a.onActivityCreated(DocumentActivity.java:78)
at e.a(FragmentManager.java:891)
at e.a(FragmentManager.java:1080)
at e.a(FragmentManager.java:1062)
at e.k(FragmentManager.java:1810)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:501)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133)
at android.app.Activity.performStart(Activity.java:4549)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2022)
... 11 more
第 654 行是此方法的第一行:
public static Document get(long id, Context context) {
DocumentEntity entity = DatabaseHelper.getInstance().getDocumentDao().queryForId((int)id);
Document doc = new Document(entity, context);
return doc;
}
getDocumentDao 返回一个类型的对象com.j256.ormlite.dao.RuntimeExceptionDao