1

Hi I am using ORMLite for database access. After I add a table with an image it crashes with a ClassNotFoundException.

@DatabaseField (dataType = DataType.SERIALIZABLE)
private Image image;

The following error occured in the onCreate method of the subclass of OrmLiteSqliteOpenHelper.

01-09 14:45:51.108: W/dalvikvm(1897): Unable to match class for part: 'Landroid/media/Image;'
01-09 14:45:51.118: W/dalvikvm(1897): threadid=12: thread exiting with uncaught exception (group=0x40a13300)
01-09 14:45:51.198: E/AndroidRuntime(1897): FATAL EXCEPTION: AsyncTask #1
01-09 14:45:51.198: E/AndroidRuntime(1897): java.lang.RuntimeException: An error occured while executing doInBackground()
01-09 14:45:51.198: E/AndroidRuntime(1897):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at java.lang.Thread.run(Thread.java:856)
01-09 14:45:51.198: E/AndroidRuntime(1897): Caused by: java.lang.NoClassDefFoundError: android/media/Image
01-09 14:45:51.198: E/AndroidRuntime(1897):     at java.lang.Class.getDeclaredFields(Native Method)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at java.lang.Class.getDeclaredFields(Class.java:647)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at com.j256.ormlite.android.DatabaseTableConfigUtil.fromClass(DatabaseTableConfigUtil.java:49)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at com.j256.ormlite.db.SqliteAndroidDatabaseType.extractDatabaseTableConfig(SqliteAndroidDatabaseType.java:76)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at com.j256.ormlite.dao.DaoManager.createDao(DaoManager.java:67)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at com.j256.ormlite.table.TableUtils.createTable(TableUtils.java:218)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at com.j256.ormlite.table.TableUtils.createTable(TableUtils.java:53)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at com.example.test.ECDatabaseHelper.onCreate(ECDatabaseHelper.java:84)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.onCreate(OrmLiteSqliteOpenHelper.java:207)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at com.j256.ormlite.android.AndroidConnectionSource.getReadWriteConnection(AndroidConnectionSource.java:63)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at com.j256.ormlite.android.AndroidConnectionSource.getReadOnlyConnection(AndroidConnectionSource.java:51)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at com.j256.ormlite.dao.BaseDaoImpl.queryForFirst(BaseDaoImpl.java:229)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at com.example.test.ECDatabaseHelper.getLastUpdateStations(ECDatabaseHelper.java:218)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at com.example.test.ECConnect.doUpdate(ECConnect.java:170)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at com.example.test.ECConnect$UpdateTask.doInBackground(ECConnect.java:109)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at com.example.test.ECConnect$UpdateTask.doInBackground(ECConnect.java:1)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-09 14:45:51.198: E/AndroidRuntime(1897):     ... 5 more
01-09 14:45:51.198: E/AndroidRuntime(1897): Caused by: java.lang.ClassNotFoundException: android.media.Image
01-09 14:45:51.198: E/AndroidRuntime(1897):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
01-09 14:45:51.198: E/AndroidRuntime(1897):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
01-09 14:45:51.198: E/AndroidRuntime(1897):     ... 25 more

Can someone help me out? I tried ORMLite 4.48 and 4.47.

add ECDatabaseHelper:

public class ECDatabaseHelper extends OrmLiteSqliteOpenHelper    {

    private final String LOG_TAG = getClass().getSimpleName();
    private static final String DATABASE_NAME = "test.db";
    private static final int DATABASE_VERSION = 1;
    private static ECDatabaseHelper  instance = null;

    private Dao<Station, Integer> stationDao = null;
    private Dao<Service, Integer> serviceDao = null;

    public static synchronized ECDatabaseHelper getHelper(Context context) {
        Log.i("ECDatabase", "getHelper");
        if (instance == null) {
            instance = new ECDatabaseHelper(context);
        }
        return instance;
    }

    private ECDatabaseHelper(Context context) {
        super(context.getApplicationContext(), DATABASE_NAME, null, DATABASE_VERSION);
        Log.i("ECDatabase", "ECDatabaseHelper () - constructor");
    }

    @Override
    public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
            try {
                    Log.i(ECDatabaseHelper.class.getName(), "onCreate");
                    TableUtils.createTable(connectionSource, Station.class);
                    Log.i(ECDatabaseHelper.class.getName(), "table station created");
                    TableUtils.createTable(connectionSource, Service.class);
                    Log.i(ECDatabaseHelper.class.getName(), "table service created");
            } catch (SQLException e) {
                    Log.e(ECDatabaseHelper.class.getName(), "Can't create database", e);
                    throw new RuntimeException(e);
            }

    }
OroshiX
  • 712
  • 1
  • 8
  • 28
corban
  • 618
  • 6
  • 16

1 Answers1

1

i think there is problem with external Jar creating folder named libs & copy and paste all needed jar files in that folder see this link to do this

for saving image you need store byte[] fields in ORMLite

@DatabaseField(dataType = DataType.BYTE_ARRAY)
byte[] imageBytes;

for more info see this link

Community
  • 1
  • 1
Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
  • I had added the jars to libs folder in the project. The first table (without image) workes fine! But the new services table raise the error. – corban Jan 09 '14 at 14:35
  • can you create two table without image? did you test this?i mean create two class without image and create DB for those, did try this? – Shayan Pourvatan Jan 09 '14 at 14:40
  • I've just commented out the image field and then it worked with two tables. Maybe I should switch to a byte[] for my image. – corban Jan 09 '14 at 14:45
  • ok it works with byte[]. It's a workaround but does not explain why images do not work. According to [link](http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_2.html#Persisted-Types) all serializable variable would work also image. – corban Jan 09 '14 at 15:03
  • `Some databases place restrictions on this field type that it cannot be the id column in a class. Other databases do not allow you to query on this type of field at all. If your database does support it, you may also have to use a Select Argument to query for this type. See section Using Select Arguments.` this is very efficient way to use byte array instead of SERIALIZABLE – Shayan Pourvatan Jan 09 '14 at 15:08