将null上的游标定型为尚未停用或关闭的游标

时间:2013-01-29 02:48:20

标签: android database sqlite cursor

在我的应用程序中,我尝试使用以下代码验证数据库中是否存在用户电子邮件地址。如果用户电子邮件地址不存在,则会将用户信息插入数据库。但是这条消息出现了,我无法将用户信息输入数据库。我不确定这里的问题是什么。

MainActivity.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();
}

DBHelper.java

    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)

2 个答案:

答案 0 :(得分:0)

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");
       c.close();
       return true;
   }
   if (c != null && !c.isClosed()) {
       c.close();
   }
   if (helper!=null){
       helper.close();
   }

   return false;

}

通过在c.close();

之前加入return true;来解决问题

答案 1 :(得分:0)

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);
try
{
    if(c.getCount()==0){  
        //not in db
       Log.i(TAG, "getCount = 0");
       c.close();   <-------- Close the cursor here.
       return true;
     }
 }
 finally
 {
     c.close();
     if (helper!=null){
        helper.close();
       }
   }
 }