我尝试在我的 Kotlin 项目
中使用 Active Android (ORM数据库)但是当我保存EditText输入时,我的程序总是崩溃!
所有程序代码均可在Gitlab.com
中找到MainActivity.kt
...
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btnMainSubmit: Button = findViewById(R.id.btn_main_submit)
val btnMainCancel: Button = findViewById(R.id.btn_main_cancel)
val edtMainType: EditText = findViewById(R.id.edt_main_type)
val edtMainName: EditText = findViewById(R.id.edt_main_name)
val edtMainInfo: EditText = findViewById(R.id.edt_main_info)
val txvMainShow: TextView = findViewById(R.id.txv_main_show)
btnMainSubmit.setOnClickListener{
var name:String = edtMainName.text.toString()
var type:String = edtMainType.text.toString()
var info:String = edtMainInfo.text.toString()
val model:ModelRequest = ModelRequest()
model.adItems(type,name,info)
model.save()
}
btnMainCancel.setOnClickListener{
var model:ModelRequest = ModelRequest.random
txvMainShow.text = model.mainName + " - " + model.mainInfo + " - " + model.mainType
}
}
}
AppController.kt
...
open class DatabaseTestCase : Application() {
val DB_NAME = "test.db"
fun setUp() {
val configuration = Configuration.Builder(getContext())
.setDatabaseName(DB_NAME)?
.setDatabaseVersion(1)?
.create()
ActiveAndroid.initialize(configuration)
}
fun tearDown() {
getContext()?.deleteDatabase(DB_NAME)
ActiveAndroid.dispose()
}
}
ModelRequest.kt
@Table(name = "model_request")
open class ModelRequest : Model {
@Column(name = "main_type") var mainType: String? = null
@Column(name = "main_name") var mainName: String? = null
@Column(name = "main_info") var mainInfo: String? = null
val all: List<ModelRequest> get() = getMany(ModelRequest::class.java, "name")
constructor() : super() {}
constructor(main_type: String, main_name: String, main_info: String) {
this.mainType = main_type
this.mainName = main_name
this.mainInfo = main_info
}
companion object {
val random: ModelRequestget() = Select().from(ModelRequest::class.java).executeSingle()
val mainName: String
get() {
val ModelRequest = Select().from(ModelRequest::class.java).executeSingle<ModelRequest>()
return ir.devapp.mlife.model.ModelRequest.mainName
}
val driverInfo: ModelRequest
get() = Select().from(ModelRequest::class.java).executeSingle()
}
fun adItems(main_type: String, main_name: String, main_info: String){
this.mainType = main_type
this.mainName = main_name
this.mainInfo = main_info
}
}