用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;
}
}
}