java.lang.NoSuchMethodException: <init> []

java.lang.NoSuchMethodException: <init> []

我使用的是 kotlin,当我尝试使用 sugarORM 获取数据时遇到了一些错误

D/SQL Log: SQLiteQuery: SELECT * FROM PRODUCT_SUGAR_ORM W/System.err: java.lang.NoSuchMethodException: <init> []
 at java.lang.Class.getConstructor(Class.java:531)
 at java.lang.Class.getDeclaredConstructor(Class.java:510)
 at com.orm.SugarRecord.getEntitiesFromCursor(SugarRecord.java:212)
 at com.orm.SugarRecord.find(SugarRecord.java:204)
 at com.orm.SugarRecord.listAll(SugarRecord.java:127)

这是我的代码

data class ProductSugarORM(
        var base_price: Double,
        var category: Int,
        val created_on: String,
        var description: String,
        val id: Int,
        var name: String,
        var sell_price: Double,
        var stock: Int,
        val store: Int,
        val type: String,
        val updated_on: String
):SugarRecord(){
    fun ProductSugarORM(){}
}

我想使用 Log.w("load-local", "${SugarRecord.listAll(ProductSugarORM::class.java)}")

查看结果

请注意 in the documentation 您的实现需要一个空的构造函数。您将需要添加一个空的构造函数。这意味着为所有这些参数添加默认值,这可能没有意义。

无论如何,我不确定你能否摆脱数据 class,因为它们是不可变的,而且 Sugar 似乎在幕后使用了一些可能需要可变性的反射(不知道) .