我正在使用带有sqlite的ormlite。但我们得到的错误就像:
07-11 12:39:10.680:E / AndroidRuntime(1415):致命异常:主要 07-11 12:39:10.680:E / AndroidRuntime(1415):流程: com.mmd.testormlite,PID:1415 07-11 12:39:10.680: E / AndroidRuntime(1415):java.lang.IllegalStateException:不能 找到OpenHelperClass,因为没有类的泛型参数 com.mmd.testormlite.MainActivity类扩展 OrmLiteSqliteOpenHelper。你应该使用getHelper(Context,Class) 代替。 07-11 12:39:10.680:E / AndroidRuntime(1415):at com.j256.ormlite.android.apptools.OpenHelperManager.lookupHelperClass(OpenHelperManager.java:273) 07-11 12:39:10.680:E / AndroidRuntime(1415):at com.j256.ormlite.android.apptools.OpenHelperManager.getHelper(OpenHelperManager.java:105) 07-11 12:39:10.680:E / AndroidRuntime(1415):at data.DatabaseManager.getHelper(DatabaseManager.java:17)07-11 12:39:10.680:E / AndroidRuntime(1415):at data.Repo。(Repo.java:13)07-11 12:39:10.680: E / AndroidRuntime(1415):at com.mmd.testormlite.MainActivity $ 1.onClick(MainActivity.java:28)07-11 12:39:10.680:E / AndroidRuntime(1415):at android.view.View.performClick(View.java:4438)07-11 12:39:10.680: E / AndroidRuntime(1415):at android.view.View $ PerformClick.run(View.java:18422)07-11 12:39:10.680:E / AndroidRuntime(1415):at android.os.Handler.handleCallback(Handler.java:733)07-11 12:39:10.680:E / AndroidRuntime(1415):at android.os.Handler.dispatchMessage(Handler.java:95)07-11 12:39:10.680:E / AndroidRuntime(1415):at android.os.Looper.loop(Looper.java:136)07-11 12:39:10.680: E / AndroidRuntime(1415):at android.app.ActivityThread.main(ActivityThread.java:5017)07-11 12:39:10.680:E / AndroidRuntime(1415):at java.lang.reflect.Method.invokeNative(Native Method)07-11 12:39:10.680:E / AndroidRuntime(1415):at java.lang.reflect.Method.invoke(Method.java:515)07-11 12:39:10.680: E / AndroidRuntime(1415):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 07-11 12:39:10.680:E / AndroidRuntime(1415):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)07-11 12:39:10.680:E / AndroidRuntime(1415):at dalvik.system.NativeStart.main(原生方法)
DatabaseManager类:
public class DatabaseManager<H extends OrmLiteSqliteOpenHelper> {
private H helper;
public H getHelper(Context context)
{
if(helper == null)
{
helper = (H) OpenHelperManager.getHelper(context);//The method getHelper(Context) from the type OpenHelperManager is deprecated. why ?
}
return helper;
}
public void releaseHelper(H helper)
{
if (helper != null) {
OpenHelperManager.release();//The method getHelper(Context) from the type OpenHelperManager is deprecated. why ?
helper = null;
}
}
}
DatabaseHelper类:
public class DatabaseHelper extends OrmLiteSqliteOpenHelper{
private static final String DATABASE_NAME = "db.sqlite";
private static final int DATABASE_VERSION = 1;
private Dao<News, Integer> userDao = null;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
DatabaseInitializer initializer = new DatabaseInitializer(context);
try {
initializer.createDatabase();
initializer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
try {
Log.i(DatabaseHelper.class.getName(), "onCreate");
TableUtils.createTable(connectionSource, News.class);
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
throw new RuntimeException(e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
try {
Log.i(DatabaseHelper.class.getName(), "onUpgrade");
TableUtils.dropTable(connectionSource, News.class, true);
onCreate(db);
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
throw new RuntimeException(e);
}
}
@Override
public void close() {
super.close();
userDao = null;
}
public Dao<News, Integer> getNewsDao() {
if (userDao == null) {
try {
userDao =DaoManager.createDao(getConnectionSource(), News.class);
} catch (SQLException e) {
e.printStackTrace();
}
}
return userDao;
}
}
答案 0 :(得分:5)
这是错误中的重要一行:
E / AndroidRuntime(1415):java.lang.IllegalStateException:找不到OpenHelperClass,因为类com.mmd.testormlite.MainActivity的通用参数都没有扩展OrmLiteSqliteOpenHelper。您应该使用getHelper(Context,Class)。
OrmLite正在尝试使用您传入的上下文(在本例中为MainActivity
)作为OpenHelperClass 。
来自ORMLite documentation for OpenHelperManager.getHelper(Context ctx)
:
与
getHelper(Context, Class)
(推荐)类似,但我们必须通过其他方式找到帮助程序类。此方法要求Context是一个扩展ORMLite的基类之一的类,例如OrmLiteBaseActivity
。需要在strings.xml中设置或者辅助类。
所以,你有四个选择:
OrmLiteBaseActivity
。<string name="open_helper_classname">your.package.name.DatabaseHelper</string>
或类似的OpenHelperManager.setOpenHelperClass(Class openHelperClass)
DatabaseManager
课程中提供具体的班级名称,如下所示:helper = OpenHelperManager.getHelper(context, DatabaseHelper.class);
最后,您要问为什么OpenHelperManager.release();
已被弃用。我不确定为什么它已被弃用(文档表明这可能仅仅是命名/约定更改),但您应该使用OpenHelperManager.releaseHelper();
代替。