简体   繁体   English

无法使用 SqliteDatabaseHelper 在 Android 工作室中开始活动

[英]Can't start activity in Android studio using SqliteDatabaseHelper

I'm trying to create a simple application and I'm storing my data in a SQLiteDatabase, but the problem is when I type my credentials the application shut down immediatly without showing anything on logcat.我正在尝试创建一个简单的应用程序,并将数据存储在 SQLiteDatabase 中,但问题是当我输入凭据时,应用程序立即关闭,而 logcat 上没有显示任何内容。 Is it possible to call our database and work on it out of onCreate method in our main activiy?是否可以在我们的主要活动中调用我们的数据库并使用onCreate方法对其进行处理? Here's my MainActivity code:这是我的MainActivity代码:

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.username)
    EditText username;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.buttonConnect)
    Button buttonConnect;

    //Calling our database
    final DatabaseHelper dbHelper = new DatabaseHelper(this);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.buttonConnect)
    public void onConnect() {
        if (!emptyFields()) {
            User user = dbHelper.queryUser(username.getText().toString(), password.getText().toString());
            if (user != null) {
                Intent intent = new Intent(MainActivity.this, RestaurantCategories.class);
                startActivity(intent);
                Toast.makeText(MainActivity.this, "Welcome " + user.getUsername(), Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this, "User not found", Toast.LENGTH_SHORT).show();
                password.setText("");
            }
        }else{
            Toast.makeText(MainActivity.this, "Empty Fields", Toast.LENGTH_SHORT).show();
        }
    }

    private boolean emptyFields() {
        if (TextUtils.isEmpty(username.getText().toString()) || TextUtils.isEmpty(password.getText().toString())) {
            return true;
        }else {
            return false;
        }
    }
}

and this My Database queries codes:这个我的数据库查询代码:

public class DatabaseHelper extends SQLiteOpenHelper {

    public DatabaseHelper(Context context) {
        super(context,DatabaseOptions.DB_NAME, null,DatabaseOptions.DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //Create table
        db.execSQL(DatabaseOptions.CREATE_USERS_TABLE_);

        // Inserting our login credentials
        initialInsertion(db,"aladin","alloallo123");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + DatabaseOptions.USERS_TABLE);
        // Create tables again
        onCreate(db);
    }


    public void initialInsertion(SQLiteDatabase db,String username,String password) {
        ContentValues values = new ContentValues();
        values.put(DatabaseOptions.USERNAME,username);
        values.put(DatabaseOptions.PASSWORD,password);
        db.insert(DatabaseOptions.USERS_TABLE,null,values);
        db.close();
    }

    // To verify credentials in our login page !
    public User queryUser(String username, String password) {

        SQLiteDatabase db = this.getReadableDatabase();
        User user = null;

        Cursor cursor = db.query(DatabaseOptions.USERS_TABLE, new String[]{DatabaseOptions.ID,
                        DatabaseOptions.USERNAME, DatabaseOptions.PASSWORD}, DatabaseOptions.USERNAME
                        + "=? and " + DatabaseOptions.PASSWORD + "=?",
                new String[]{username, password}, null, null, null, "1");
        if (cursor != null)
            cursor.moveToFirst();
        if (cursor != null && cursor.getCount() > 0) {
            user = new User(cursor.getString(1), cursor.getString(2));
        }
        // return user
        return user;
    }


}

The Idea is since I don't have a register page I want to insert directly to my database in the methode OnCreate that's why I'm calling the function initialInsertion想法是因为我没有注册页面,所以我想在OnCreate方法中直接插入我的数据库,这就是为什么我调用 function initialInsertion

Ps: DatabaseOptions is a different class where I create database & tables.. Ps: DatabaseOptions是一个不同的 class 我在其中创建数据库和表..

If there is a crash in JVM code, there's also a stacktrace to go with it.如果 JVM 代码中出现崩溃,那么还有一个指向 go 的堆栈跟踪。 You might have a logcat filter or something that hides it though.您可能有一个 logcat 过滤器或隐藏它的东西。

In your initialInsertion() you're closing the database you didn't open yourself.在您的initialInsertion()中,您正在关闭您自己没有打开的数据库。 That's an error.那是一个错误。 Remove the close() call there.删除那里的close()调用。

(That database onCreate() code path gets triggered by the getReadableDatabase() in your queryUser() - that's what you'd be seeing in the stacktrace.) (该数据库onCreate()代码路径由您的getReadableDatabase()中的queryUser()触发 - 这就是您在堆栈跟踪中看到的内容。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM