解决方案 #1 -在设置中禁用即时运行
解决方案 #2 - 使用反射从 BootstrapApplication 获取真正的应用程序
public static CustomApplication getRealApplication (Context applicationContext)
{
CustomApplication application = null;
if (applicationContext instanceof CustomApplication)
{
application = (CustomApplication) applicationContext;
}
else
{
Application realApplication = null;
Field magicField = null;
try
{
magicField = applicationContext.getClass().getDeclaredField("realApplication");
magicField.setAccessible(true);
realApplication = (Application) magicField.get(applicationContext);
}
catch (NoSuchFieldException e)
{
Log.e(TAG, e.getMessage());
}
catch (IllegalAccessException e)
{
Log.e(TAG, e.getMessage());
}
application = (CustomApplication) realApplication;
}
return application;
}
在某处使用:
Context applicationContext = getContext().getApplicationContext();
CustomApplication application = getRealApplication(applicationContext);
使用示例:
public class MyProvider extends OrmLiteProvider<OrmLiteSqliteOpenHelper, OrmLiteUriMatcher<OrmLiteMatcherEntry>>
{
@Override
protected OrmLiteSqliteOpenHelper createHelper ()
{
Context applicationContext = getContext().getApplicationContext();
CustomApplication application = CustomApplication.getRealApplication(applicationContext);
return application.getComponent().databaseHelper();
}
...
}