Orm Lite - Could not find a public constructor that has a single (context) argument for the helper class

I am using OrmLite 4.47. I followed many tutorials and read other questions about stackoverflow, but I cannot figure out how to solve this problem.

what's the full 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] 

What is my base database 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 had a problem when in my work I try to do this

 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.

+6
source share
5 answers

Try cleaning the project ... it's weird because the code looks good enaugh to work :)

+1
source

For those who encounter 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 it out too fooobar.com/questions/246716 / ...

+4
source

Check out the import part of your class that uses the MyDatabaseHelper class. Your getHelper method seems to use the 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 the MyDatabaseHelper class correctly, it should be

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

To explain the error

By default, the OrmLiteSqliteOpenHelper class has only a constructor, as shown below, it does not have a constructor that has one argument (context).

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

Therefore, this may cause an IllegalStateException error.

0
source

Create an empty constructor in the POJO or bean class. Solution:)

EX.

 public Truck(){ //empty constructor in custom class } 
0
source

Well, as I was able to solve my problem, the rules of the procurator did not work for me; So, what I did, I created the constructor of the DatabaseHelper class as open, and then initialized the DatabaseHelper object, passing the Activity context to its constructor, in the onCreate () activity method, where I wanted to receive data or send;

This is a public constructor,

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

Here I initialized the class object:

  databaseHelper = new DatabaseHelper(this); 
0
source

Source: https://habr.com/ru/post/969346/


All Articles