Reputation: 154
I faced an error related to Int:
[kapt] An exception occurred: java.lang.IllegalArgumentException: int cannot be converted to an Element
I don't understand why it happens. Also I couldn't find solution in similar questions.I think this error means that the dao can't deal with Int and can't update the database.
Here is my code.
@Entity(tableName = "Promise")
data class Promise(
@PrimaryKey(autoGenerate = true) val index : Int,
@ColumnInfo(name = "date") val date : Int,
@ColumnInfo(name = "content") val content : String
)
@Dao
interface PromiseDao {
@Query("Select * from Promise")
fun getAll() :List<Promise>
@Query("select * from Promise order by date DESC limit 1")
fun getRecent() :Promise
@Insert
fun insertPromise(date: Int, content:String)
}
@Database(entities = arrayOf(Promise::class), version = 1)
abstract class PromiseDatabase : RoomDatabase(){
abstract fun promiseDao() : PromiseDao
companion object{
private var INSTANCE : PromiseDatabase? = null
fun getInstance(context : Context) : PromiseDatabase{
var tmpPromiseDB = INSTANCE
if(tmpPromiseDB == null){
tmpPromiseDB = Room.databaseBuilder(context.applicationContext, PromiseDatabase::class.java, "promise_database").build()
INSTANCE = tmpPromiseDB
}
return tmpPromiseDB
}
}
}
class PostActivity : AppCompatActivity() {
lateinit var promiseDatabase : PromiseDatabase
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_post)
promiseDBinit()
val cancelPost_imageView = findViewById(R.id.cancelPost_button) as ImageView
val postHead_textView = findViewById(R.id.postHead_textView) as TextView
val writePost_button = findViewById(R.id.writePost_button) as Button
cancelPost_imageView.setOnClickListener(View.OnClickListener {
onBackPressed()
})
writePost_button.setOnClickListener(View.OnClickListener {
//DB에 올리는 과정
var content = StringBuilder()
content.append(postHead_textView.text.toString())
promiseDatabase.promiseDao().insertPromise(System.currentTimeMillis().toInt(), content.toString())
})
}
fun promiseDBinit(){
promiseDatabase = PromiseDatabase.getInstance(applicationContext)
}
}
Upvotes: 0
Views: 176
Reputation: 5113
Check Room @Insert syntax
Change this:
@Insert
fun insertPromise(date: Int, content:String)
to this:
@Insert
fun insertPromise(promise: Promise)
And this:
promiseDatabase.promiseDao().insertPromise(System.currentTimeMillis().toInt(), content.toString())
to this:
promiseDatabase.promiseDao().insertPromise(Promise(System.currentTimeMillis().toInt(), content.toString()))
Upvotes: 1