Я попытался добавить данные в SQLite после this . Тем не менее, я получаю null
, когда я добавляю данные
DatabaseHandler.kt
override fun onCreate(db: SQLiteDatabase) {
val CREATE_TABLE = "CREATE TABLE $TABLE_NAME (" +
ID + " INTEGER PRIMARY KEY," +
NAME + " TEXT," + DESC + " TEXT," +
COMPLETED + " TEXT);"
db.execSQL(CREATE_TABLE)
}
fun addTask(validasi: Validasi): Boolean {
val db = this.writableDatabase
val values = ContentValues()
values.put(NAME, validasi.name)
values.put(DESC, validasi.desc)
val _success = db.insert(TABLE_NAME, null, values)
db.close()
return (Integer.parseInt("$_success") != -1)
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
val DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME
db.execSQL(DROP_TABLE)
onCreate(db)
}
companion object {
private val DB_VERSION = 1
private val DB_NAME = "MyTasks"
private val TABLE_NAME = "Tasks"
private val ID = "Id"
private val NAME = "Name"
private val DESC = "Desc"
private val COMPLETED = "Completed"
}
ResultActivity.kt
class ResultActivity : AppCompatActivity() {
var dbHandler: DatabaseHandler? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_result)
button_save.setOnClickListener(){
var success: Boolean = false
val validasi: Validasi = Validasi()
validasi.name = "1"
validasi.desc = "1"
validasi.completed = "y"
Toast.makeText(this@ResultActivity, ""+validasi.name, Toast.LENGTH_SHORT).show()
success = dbHandler?.addTask(validasi) as Boolean
if (success)
finish()
}
}
но когда я нажимаю кнопку, приложение останавливается и журнал говорит
FATAL EXCEPTION: main
Process: com.wahanaartha.supervisionline, PID: 930
kotlin.TypeCastException: null cannot be cast to non-null type kotlin.Boolean
at ResultActivity
Что происходит в этой строке: success = dbHandler?.addTask(validasi) as Boolean