Orm Lite - 无法找到具有helper类的单个(Context)参数的公共构造函数

时间:2014-05-15 17:40:01

标签: android ormlite

我使用OrmLite 4.47。 我遵循了许多教程,并在stackoverflow上阅读了其他问题,但我无法理解如何解决这个问题。

这是完整的消息

05-15 16:36:13.805: E/AndroidRuntime(15382): Caused by: java.lang.IllegalStateException: Could not find public constructor that has a single (Context) argument for helper class class com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper
05-15 16:36:13.805: E/AndroidRuntime(15382): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context]

那是我的databaseHelper Class

public class MyDatabaseHelper extends OrmLiteSqliteOpenHelper {  

    // name of the database file for your application -- change to something  
    // appropriate for your app  
    private static final String DATABASE_NAME = "databas.db";  
    // any time you make changes to your database, you may have to increase the  
    // database version  
    private static final int DATABASE_VERSION = 1;  

    //genera molte eccezioni
    private Dao<Truck, Integer> truckDao = null;

    //genera una sola eccezione a runtime
    private RuntimeExceptionDao<Truck, Integer> truckRuntimeDao=null;

    public MyDatabaseHelper(Context context) { 
        super(context, DATABASE_NAME, null, DATABASE_VERSION); 

    } 

    @Override
    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
        // TODO Auto-generated method stub
        try {
            TableUtils.clearTable(connectionSource, Truck.class);
        } catch (java.sql.SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int OldVersion,
            int newVersion) {
        // TODO Auto-generated method stub
        try {
            TableUtils.dropTable(connectionSource, Truck.class, true);
            onCreate(database,connectionSource);
        } catch (java.sql.SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }  

    public Dao<Truck, Integer> getTruckDao() throws java.sql.SQLException{
        if(truckDao==null){
            truckDao=getDao(Truck.class);
        }
        return truckDao;
    }

    public RuntimeExceptionDao<Truck, Integer> getTruckRuntimeExceptionDao(){
        if(truckRuntimeDao==null){
            truckRuntimeDao=getRuntimeExceptionDao(Truck.class);
        }
        return truckRuntimeDao;
    }
}

我在尝试这样做的时候遇到了问题

MyDatabaseHelper helper = OpenHelperManager.getHelper(this,MyDatabaseHelper.class);
RuntimeExceptionDao<Truck, Integer> truckDao = helper.getTruckRuntimeExceptionDao();

因此数据库助手类是公共的,而Activity类扩展了Activiy。

5 个答案:

答案 0 :(得分:7)

对于那些在启用minify时遇到此错误的人(proguard):

为ormlite添加以下配置:

REPLACE record

也请查看Stackoverflow - proguard-with-ormlite-on-android

答案 1 :(得分:1)

尝试清理项目......这很奇怪,因为代码看起来很好用于工作:)

答案 2 :(得分:0)

检查使用MyDatabaseHelper类的类的导入部分。 看来你的getHelper方法使用了错误的默认构造函数。

您的错误消息

java.lang.IllegalStateException: Could not find public constructor that has a single (Context) argument for helper class class com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper

如果正确使用MyDatabaseHelper类,则应为

java.lang.IllegalStateException: Could not find public constructor that has a single (Context) argument for helper class class 'yourpackage'.MyDatabaseHelper

为了解释错误,

默认OrmLiteSqliteOpenHelper类只有如下构造函数, 它没有一个具有单个参数(context)的构造函数。

public DatabaseHelper(Context context, SQLiteDatabase.CursorFactory factory) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }

因此可能导致IllegalStateException错误。

答案 3 :(得分:0)

在POJO或bean类中创建一个空构造函数。问题解决:))

EX。

public Truck(){

//empty constructor in custom class

}

答案 4 :(得分:0)

好的,我怎么能够解决我的问题,而且规则对我来说没有用;所以我做的是,我将DatabaseHelper类构造函数设置为public,然后我初始化了DatabaseHelper对象,将Activity上下文传递给它的构造函数,在onCreate()我想要获取数据或发送的活动方法;

这是公共构造函数,

 public DatabaseHelper(Context context)
        {
            super(context,DB_NAME,null,DB_VERSION,R.raw.ormlite_config);
        }

这里我初始化了上面的类对象:

  databaseHelper = new DatabaseHelper(this);