ORMLite does not know how to store class java.util.ArrayList错误的解决

程序中要用开源库ORMLite保存一个自定义类的数据,其中这个自定义类有如下属性:

    @DatabaseField
    private String packageName;

    @DatabaseField
    private int id;

    @DatabaseField
    private int type;

    @DatabaseField
    private ArrayList<Feature> featureList;

这些属性都加上了ormLite的@DatabaseField标注。Feature也是一个自定义的类,实现了Serializable接口。

但是在运行后报了如下错误:

 E/DatabaseHelper: Unable to getDao
                      java.sql.SQLException: ORMLite does not know how to store class java.util.ArrayList for field 'featureList'.  Use another class, custom persister, or to serialize it use dataType=DataType.SERIALIZABLE
               at com.j256.ormlite.field.FieldType.<init>(FieldType.java:185)
               at com.j256.ormlite.table.DatabaseTableConfig.convertFieldConfigs(DatabaseTableConfig.java:236)
               at com.j256.ormlite.table.DatabaseTableConfig.extractFieldTypes(DatabaseTableConfig.java:101)
               at com.j256.ormlite.dao.BaseDaoImpl.initialize(BaseDaoImpl.java:153)
               at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:128)
               at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:119)
               at com.j256.ormlite.dao.BaseDaoImpl$5.<init>(BaseDaoImpl.java:921)
               at com.j256.ormlite.dao.BaseDaoImpl.createDao(BaseDaoImpl.java:921)
               at com.j256.ormlite.dao.DaoManager.createDao(DaoManager.java:72)
               at com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.getDao(OrmLiteSqliteOpenHelper.java:279)

从报错信息上看,ormLite不知道怎么存储这个ArrayList 类型的featureList对象。同时也给出了解决方案,即加上dataType=DataType.SERIALIZABLE。

那么怎么加上呢?查找文档,修改如下:

    @DatabaseField(dataType = DataType.SERIALIZABLE)
    private ArrayList<Feature> featureList;

问题就解决了。

猜你喜欢

转载自blog.csdn.net/fenggering/article/details/78841927
今日推荐