Try to clean the project...it's strange because the code looks good enaugh to work :)
Orm Lite - Could not find public constructor that has a single (Context) argument for helper class
Question
I'm using OrmLite 4.47. I followed many tutorials, and read others questions on stackoverflow, but I can't understand how to solve this problem.
that's the complete message
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]
That's my 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;
}
}
And I got the problem when in my activity i try to do that
MyDatabaseHelper helper = OpenHelperManager.getHelper(this,MyDatabaseHelper.class);
RuntimeExceptionDao<Truck, Integer> truckDao = helper.getTruckRuntimeExceptionDao();
So the database helper class is public, and the Activity class extends Activiy.
Solution 2
OTHER TIPS
For those that run into this error with minify enabled (proguard):
Add the following configuration for ormlite:
# ormlite
-keep class com.j256.**
-keepclassmembers class com.j256.** { *; }
-keep enum com.j256.**
-keepclassmembers enum com.j256.** { *; }
-keep interface com.j256.**
-keepclassmembers interface com.j256.** { *; }
-keepclassmembers class * {
public <init>(android.content.Context);
}
Check this out too Stackoverflow - proguard-with-ormlite-on-android
Check the import part of your class which uses MyDatabaseHelper class. It seems that your getHelper method uses a wrong default constructor.
Your error message,
java.lang.IllegalStateException: Could not find public constructor that has a single (Context) argument for helper class class com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper
If you use MyDatabaseHelper class properly , it should be
java.lang.IllegalStateException: Could not find public constructor that has a single (Context) argument for helper class class 'yourpackage'.MyDatabaseHelper
For explaining error,
Default OrmLiteSqliteOpenHelper class only has a constructor like below, it doesn't have a constructor which has a single argument (context).
public DatabaseHelper(Context context, SQLiteDatabase.CursorFactory factory) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
So it could cause an IllegalStateException error.
Create an empty constructor in your POJO or bean class. Problem solve :)
EX.
public Truck(){
//empty constructor in custom class
}
OK this how I was able to solve my problem progurad-rules didn't work for me; So what I did was, I made DatabaseHelper class constructor as public and then I initialized DatabaseHelper object passing Activity context into its constructor, in onCreate() Method of activity where I wanted to fetch data or send;
This is the public constructor,
public DatabaseHelper(Context context)
{
super(context,DB_NAME,null,DB_VERSION,R.raw.ormlite_config);
}
Here I intialized above class object:
databaseHelper = new DatabaseHelper(this);