gpt4 book ai didi

android - 当 ORMLite 尝试在 android 上创建带有图像的数据库表时出现 ClassNotFoundException

转载 作者:行者123 更新时间:2023-11-30 03:10:43 24 4
gpt4 key购买 nike

您好,我正在使用 ORMLite 进行数据库访问。在我添加带有图像的表格后,它因 ClassNotFoundException 而崩溃。

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

OrmLiteSqliteOpenHelper 子类的onCreate 方法出现如下错误。

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

有人能帮帮我吗?我试过 ORMLite 4.48 和 4.47。

添加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);
}

}

最佳答案

我认为创建名为 libs 的文件夹的外部 Jar 存在问题,并将所有需要的 jar 文件复制并粘贴到该文件夹​​中,请参阅 this link做这个

为了保存图像,你需要在 ORMLite 中存储 byte[] 字段

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

有关详细信息,请参阅 this link

关于android - 当 ORMLite 尝试在 android 上创建带有图像的数据库表时出现 ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21022704/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com