Android 在未停用或关闭的null上终结光标
在我的应用程序中,我尝试使用以下代码验证数据库中是否存在用户电子邮件地址。如果用户电子邮件地址不存在,它将把用户信息插入数据库。但是出现了此消息,我无法将用户信息输入数据库。我不太确定这里的问题是什么 MainActivity.javaAndroid 在未停用或关闭的null上终结光标,android,database,sqlite,cursor,Android,Database,Sqlite,Cursor,在我的应用程序中,我尝试使用以下代码验证数据库中是否存在用户电子邮件地址。如果用户电子邮件地址不存在,它将把用户信息插入数据库。但是出现了此消息,我无法将用户信息输入数据库。我不太确定这里的问题是什么 MainActivity.java public void onCreate(){ helper = new DBHelper(this); Button loginButton = (Button) findViewById(R.id.login_btn); login
public void onCreate(){
helper = new DBHelper(this);
Button loginButton = (Button) findViewById(R.id.login_btn);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
if(useremail.length() == 0){
alertMessage = "Please enter Email Address.";
dialogBox();
}
else if (password.length() == 0){
alertMessage = "Please enter Password.";
dialogBox();
}
else {
emailid = useremail.getText().toString();
userpassword = password.getText().toString();
if(!helper.emailidChecking(emailid)){
helper.insert_wbm_user(emailid);
}
else{
alertMessage="User email exists";
dialogBox();
}
}
}
});
}
public void onDestroy(){
super.onDestroy();
helper.close();
}
public boolean emailidChecking(String emailid) throws SQLException
{
helper = this.getReadableDatabase();
Log.i(TAG, "emailidChecking:" +emailid);
Cursor c = helper.rawQuery("SELECT * from user_info where emailid='"+emailid+"'", null);
if(c.getCount()==0){
//not in db
Log.i(TAG, "getCount = 0");
return true;
}
if (c != null && !c.isClosed()) {
c.close();
}
if (helper!=null){
helper.close();
}
return false;
}
public long insert_user_info(String emailid) {
ContentValues cv=new ContentValues();
cv=new ContentValues();
cv.put("emailid", emailid);
Log.i(TAG, "insert db");
long createdinMsg = getWritableDatabase().insert("user_info", "emailid", cv);
return createdinMsg;
}
DBHelper.java
public void onCreate(){
helper = new DBHelper(this);
Button loginButton = (Button) findViewById(R.id.login_btn);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
if(useremail.length() == 0){
alertMessage = "Please enter Email Address.";
dialogBox();
}
else if (password.length() == 0){
alertMessage = "Please enter Password.";
dialogBox();
}
else {
emailid = useremail.getText().toString();
userpassword = password.getText().toString();
if(!helper.emailidChecking(emailid)){
helper.insert_wbm_user(emailid);
}
else{
alertMessage="User email exists";
dialogBox();
}
}
}
});
}
public void onDestroy(){
super.onDestroy();
helper.close();
}
public boolean emailidChecking(String emailid) throws SQLException
{
helper = this.getReadableDatabase();
Log.i(TAG, "emailidChecking:" +emailid);
Cursor c = helper.rawQuery("SELECT * from user_info where emailid='"+emailid+"'", null);
if(c.getCount()==0){
//not in db
Log.i(TAG, "getCount = 0");
return true;
}
if (c != null && !c.isClosed()) {
c.close();
}
if (helper!=null){
helper.close();
}
return false;
}
public long insert_user_info(String emailid) {
ContentValues cv=new ContentValues();
cv=new ContentValues();
cv.put("emailid", emailid);
Log.i(TAG, "insert db");
long createdinMsg = getWritableDatabase().insert("user_info", "emailid", cv);
return createdinMsg;
}
Logcat
01-29 10:03:12.007: D/Cursor(2143): Database path: /data/data/com.ff.fbin/databases/wbdb.db
01-29 10:03:12.007: D/Cursor(2143): Table name : null
01-29 10:03:12.007: D/Cursor(2143): SQL : SQLiteQuery: SELECT * from user_info where emailid='asd@hotmail.com'
01-29 10:03:12.007: I/dalvikvm(2143): Uncaught exception thrown by finalizer (will be discarded):
01-29 10:03:12.007: I/dalvikvm(2143): Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@405b07e8 on null that has not been deactivated or closed
01-29 10:03:12.011: I/dalvikvm(2143): at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:620)
01-29 10:03:12.011: I/dalvikvm(2143): at dalvik.system.NativeStart.run(Native Method)
01-29 10:03:12.007:D/光标(2143):数据库路径:/data/data/com.ff.fbin/databases/wbdb.db
01-29 10:03:12.007:D/光标(2143):表名:空
01-29 10:03:12.007:D/Cursor(2143):SQL:SQLiteQuery:SELECT*from user_info,其中emailid='10asd@hotmail.com'
01-29 10:03:12.007:I/dalvikvm(2143):终结器引发的未捕获异常(将被丢弃):
01-29 10:03:12.007:I/dalvikvm(2143):Ljava/lang/IllegalStateException;:正在终结游标android.database.sqlite。SQLiteCursor@405b07e8未被停用或关闭的空
01-29 10:03:12.011:I/dalvikvm(2143):位于android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:620)
01-29 10:03:12.011:I/dalvikvm(2143):在dalvik.system.NativeStart.run运行(本机方法)
通过包含c.close()解决的问题
before返回true代码>
public boolean emailidChecking(字符串emailid)引发SQLException
{
helper=this.getReadableDatabase();
Log.i(标记“emailidChecking:+emailid”);
游标c=helper.rawQuery(“从用户信息中选择*,其中emailid='”+emailid+“'”,null);
尝试
{
如果(c.getCount()==0){
//不在数据库中
Log.i(标记“getCount=0”);
c、 关闭();请修改你的代码格式。我真的不明白。你具体指的是哪一部分?我指的是额外的空行和缩进。你给出的代码不会编译,因为缺少大括号。对不起,我只是发布了与错误相关的代码,这就是为什么会有额外的黑行。更新了代码,这基本上就是在部分代码中。从帖子中删除代码是完全可以接受的(事实上是受鼓励的)。但是,这通常意味着删除整行代码,而不是留下空白。此外,您应该注意不要删除太多的大括号。