用SQLite做个一个简单的登陆注册功能,在注册时崩溃了

用SQLite做个一个简单的登陆注册功能,可以运行,然后加了一个注册是检查用户名是否已存在的函数,点注册按钮时就崩溃了,不知道原因
代码在下面,求解答啊

 package com.sylvanas.qq;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends AppCompatActivity{
    private EditText R_accountEdit;
    private EditText R_passwordEdit;
    private Button regster;
    private MyDatabaseHelper dbHelper;
    private SQLiteDatabase db;


    @Override
    protected void onCreate( Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        //Toolbar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_register);
        setSupportActionBar(toolbar);

        R_accountEdit = (EditText) findViewById(R.id.register_account);
        R_passwordEdit = (EditText) findViewById(R.id.register_password);
        regster = (Button) findViewById(R.id.register);

        dbHelper = new MyDatabaseHelper(RegisterActivity.this,"USER.db",null,2);
        db = dbHelper.getWritableDatabase();

        regster.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String R_account = R_accountEdit.getText().toString();
                String R_password = R_passwordEdit.getText().toString();

                if(CheckIsDataAlreadyInDBorNot(R_account)){
                    Toast.makeText(RegisterActivity.this,"该用户名已被注册",Toast.LENGTH_SHORT).show();
                }
                db.execSQL("insert into Login(account,password) values(?,?)",new String[]{R_account,R_password});
                Toast.makeText(RegisterActivity.this, "注册成功!", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
    public boolean CheckIsDataAlreadyInDBorNot(String value){
        db = dbHelper.getWritableDatabase();
        String Query = "Select * from userData where name =?";
        Cursor cursor = db.rawQuery(Query,new String[] { value });
        if (cursor.getCount() > 0){
            cursor.close();
            return true;
        }
        else {
            cursor.close();
            return false;
        }
    }
}

1个回答

http://download.csdn.net/download/a591326912/9514651
采纳本回答,可以代下载。

caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 留下你的邮箱地址,好给你
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问