Why occur error: Can't find a no-arg constructor for class com.example.son.kotlinandroiddemo.db.dao.M_Data?

Why occur error: Can't find a no-arg constructor for class com.example.son.kotlinandroiddemo.db.dao.M_Data?

这是我的 class DatabaseHelper:

object DatabaseHelper : OrmLiteSqliteOpenHelper(App.instance, "test.db", null, 1) {

    override fun onCreate(database: SQLiteDatabase?, connectionSource: ConnectionSource?) {
        TableUtils.createTableIfNotExists(connectionSource, Table::class.java)
        TableUtils.createTableIfNotExists(connectionSource, M_User::class.java)
        TableUtils.createTableIfNotExists(connectionSource, M_Data::class.java)
    }

    override fun onUpgrade(database: SQLiteDatabase?, connectionSource: ConnectionSource?, oldVersion: Int, newVersion: Int) {

            TableUtils.dropTable<Table, Any>(connectionSource, Table::class.java, true)
            TableUtils.dropTable<M_User, Any>(connectionSource, M_User::class.java, true)
            TableUtils.dropTable<M_Data, Any>(connectionSource, M_Data::class.java, true)
            onCreate(database, connectionSource)

    }

}

这是我的 class Table:

@DatabaseTable(tableName = "table")
data class Table(

        @DatabaseField(generatedId = true)
        var id: Int? = null,

        @DatabaseField
        var category: String = "",

        @DatabaseField
        var content: String = ""
)

class TableDao {

    companion object {
        lateinit var dao: Dao<Table, Int>
    }

    init {
        dao = DatabaseHelper.getDao(Table::class.java)
    }

    fun add(table: Table) = dao.createOrUpdate(table)

    fun update(table: Table) = dao.update(table)

    fun delete(table: Table) = dao.delete(table)

    fun queryForAll() = dao.queryForAll()

    fun removeAll() {
        for (table in queryForAll()) {
            dao.delete(table)
        }
    }

}

这是我的 class M_User:

@DatabaseTable(tableName = "m_user")
data class M_User(

        @DatabaseField(generatedId = true)
        var id: Int? = null,

        @DatabaseField
        var username: String = "",

        @DatabaseField
        var password: String = ""
)
class M_UserDao
{
    companion object {
        lateinit var dao: Dao<M_User, Int>
    }

    init {
        dao = DatabaseHelper.getDao(M_User::class.java)
    }

    fun add(table: M_User) = dao.createOrUpdate(table)

    fun update(table: M_User) = dao.update(table)

    fun delete(table: M_User) = dao.delete(table)

    fun queryForAll() = dao.queryForAll()

    fun removeAll() {
        for (table in queryForAll()) {
            dao.delete(table)
        }
    }
}

这是我的 class M_Data:

@DatabaseTable(tableName = "m_data")
data class M_Data(

        @DatabaseField(generatedId = true)
        var id: Int? = null,

        @DatabaseField
        var username: String,

        @DatabaseField
        var insert_date: String = ""
)
class M_DataDao
{
    companion object {
        lateinit var dao: Dao<M_Data, Int>
    }

    init {
        dao = DatabaseHelper.getDao(M_Data::class.java)
    }

    fun add(table: M_Data) = dao.createOrUpdate(table)

    fun update(table: M_Data) = dao.update(table)

    fun delete(table: M_Data) = dao.delete(table)

    fun queryForAll() = dao.queryForAll()

    fun removeAll() {
        for (table in queryForAll()) {
            dao.delete(table)
        }
    }

    fun getListUserAddData(username:String):List<Array<String>>
    {
          val sql="select b.username,b.insert_date from M_User a join M_Data b on a.username=b.username where a.username ='"+username +"'"
        val rawResults = dao.queryRaw(sql)

        val results = rawResults.getResults()
        return results
    }
}

我的代码将数据添加到 3 个表:

btnAdd.setOnClickListener(View.OnClickListener {
            try {
                val dao = TableDao()
                dao.add(Table(null, "1", "a1"))
                val usr= M_UserDao()
                usr.add(M_User(null,txtUserName.text.toString(),txtPassword.text.toString()))
                val data= M_DataDao()
                data.add(M_Data(null, txtUserName.text.toString(), Utilities.dateTimeNow))
            } catch (e: Exception) {
                print(e.message)
            }


        })

但出现异常:

Can't find a no-arg constructor for class com.example.son.kotlinandroiddemo.db.dao.M_Data

您的 var username: String 未初始化,因此编译器无法为您的 class 创建默认构造函数,这似乎是您正在使用的库的要求。