当我点击 kotlin(android 开发)上的提交按钮并且我使用 sqlite 作为后端时,我的应用程序就崩溃了

问题描述 投票:0回答:1

所以我正在开发应用程序,我制作了一个简单的应用程序,您可以在其中输入电子邮件和密码,并验证用户是否存在,但一旦我单击提交按钮,应用程序就会崩溃。

所以我尝试再次制作新应用程序,但错误仍然存在。我只是想知道错误。

我的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editTextTextEmailAddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textEmailAddress"
        app:layout_constraintBottom_toTopOf="@+id/editTextTextPassword"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.554" />

    <EditText
        android:id="@+id/editTextTextPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="448dp"
        android:ems="10"
        android:inputType="textPassword"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.502"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextTextPassword"
        app:layout_constraintVertical_bias="0.145" />
</androidx.constraintlayout.widget.ConstraintLayout>

我的.kt 文件:

package com.example.myapplication
import MyDatabaseHelper
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast

class MainActivity : AppCompatActivity() {
    private lateinit var dbHelper: MyDatabaseHelper
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var email=findViewById<EditText>(R.id.editTextTextEmailAddress)
        var password=findViewById<EditText>(R.id.editTextTextPassword)
        var submit=findViewById<Button>(R.id.button)
        submit.setOnClickListener{
            if(isUserExists(email.text.toString(),password.text.toString())){
                Toast.makeText(this,"Yes it okay",Toast.LENGTH_LONG)
            }
            else{
                Toast.makeText(this,"Yes it not okay",Toast.LENGTH_LONG)
            }
        }
    }
    private fun isUserExists(email: String, password: String): Boolean {
        val db = dbHelper.readableDatabase
        val cursor = db.query("user_credentials", null, "email=? AND password=?",arrayOf(email, password), null, null, null
        )
        val count = cursor .count
        cursor. close()
        return count > 0
    }
}

我的数据库帮助文件:

import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper

class MyDatabaseHelper(context: Context) :
    SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) {

    companion object {
        private const val DATABASE_NAME = "my_database.db"
        private const val DATABASE_VERSION = 1

        private const val TABLE_NAME = "user_credentials"
        private const val COLUMN_ID = "_id"
        private const val COLUMN_EMAIL = "email"
        private const val COLUMN_PASSWORD = "password"

        private const val SQL_CREATE_TABLE =
            "CREATE TABLE $TABLE_NAME (" +
                    "$COLUMN_ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    "$COLUMN_EMAIL TEXT, " +
                    "$COLUMN_PASSWORD TEXT)"

        // Add constant for password hashing algorithm, if needed
        // private const val PASSWORD_HASH_ALGORITHM = "SHA-256"
    }

    override fun onCreate(db: SQLiteDatabase) {
        db.execSQL(SQL_CREATE_TABLE)
    }

    override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
        // Placeholder for database upgrade logic
        // Example: If you need to add new columns or modify table structure,
        // you can handle the upgrade here by dropping and recreating the table,
        // or by migrating data as needed.
        // You can also implement version-specific upgrade steps based on oldVersion and newVersion.
    }

    // Add method for password hashing, if needed
    // private fun hashPassword(password: String): String {
    //     // Implement password hashing logic here
    // }
}
android kotlin sqlite backend android-drawable
1个回答
0
投票

您的应用程序似乎正在崩溃,因为您尚未在 MainActivity 中初始化 dbHelper 变量。由于您使用 MyDatabaseHelper 与 SQLite 数据库交互,因此需要在访问数据库之前对其进行初始化。

要解决此问题,请在使用 onCreate 方法中初始化 dbHelper 变量,然后再使用它。

dbHelper = MyDatabaseHelper(this) // Initialize dbHelper
© www.soinside.com 2019 - 2024. All rights reserved.