我如何使用ORM数据库" Active Android" by Kotlin在Android Studio上?

时间:2017-12-30 17:49:10

标签: android database orm kotlin activeandroid

我尝试在我的 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
    }
}

更新:

This is my StackTrace (LogCat in Android Studio 3.0.1):

And the code above points to this line of the program

0 个答案:

没有答案