cursor错误:确保Cursor在从中访问数据之前已正确初始化

时间:2015-06-18 11:38:18

标签: java android eclipse sqlite

当我在数据库中输入现有的RegId时,我收到此错误。

**DatabaseHandler.java**
package com.example.aaqib.scoolbag;

import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.content.Context;

import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

        import android.util.Log;


public class DatabaseHandler extends SQLiteOpenHelper {
//Creating a Database ScoolBag
    SQLiteDatabase mDb;
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "ScoolBag";
    public static final String KEY_NAME = "Name";
    public static final String KEY_MOBILENO = "Contact";
    public static final String KEY_REGID = "Registration";
    public static final String KEY_EMAIL = "Email";
    public static final String KEY_PASSWORD = "Password";
    // Table name
    public static final String tblReg="tblReg";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        String vQuery = "Create Table " + tblReg + "(Registration Text primary key,Name Text not null,Password Text not null,Email Text not null,Contact Text not null)";
        Log.d("StudentData", "onCreate: " + vQuery);
        db.execSQL(vQuery);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS tblReg");
        onCreate(db);
    }
//using to insert
    public void InsertRecord(String vInsertRecord) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL(vInsertRecord);
        db.close();
    }

    public void close()

    {
        mDb.close();
    }

    /*public Cursor login(String regid)throws SQLiteException
    {
        String where =(KEY_REGID + "=?");
        Cursor m = mDb.query(true, tblReg, new String[]{KEY_REGID, KEY_NAME, KEY_PASSWORD,KEY_EMAIL,KEY_MOBILENO}, where, new String[]{regid}, null, null, null, null);
        if (m != null) {
            //  m.moveToFirst();
        }
        return m;
    }*/


//fetching a emailid
    public Cursor getEmailid(String email)throws SQLiteException
    {
        String where =(KEY_EMAIL + "=?");
        Cursor m = mDb.query(true,tblReg, new String[]{KEY_REGID, KEY_NAME, KEY_PASSWORD,KEY_EMAIL,KEY_MOBILENO}, where, new String[]{email}, null, null, null, null);
        if (m != null) {
        }
        return m;
    }
 //fetching cursor id
    public Cursor getRegid(String reg)throws SQLiteException
    {
        String where =(KEY_REGID + "=?");
        Cursor j = mDb.query(true, tblReg, new String[]{KEY_REGID, KEY_NAME, KEY_PASSWORD,KEY_EMAIL,KEY_MOBILENO}, where, new String[]{reg}, null, null, null, null);
        if (j!= null) {
            //j.moveToFirst();
        }
        return j;
    }





    public DatabaseHandler open() throws SQLiteException {
        mDb = getWritableDatabase();
        return this;
    }
}

acitivity_registration.java

package com.example.aaqib.scoolbag;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import static android.view.View.OnClickListener;


public class activity_registration extends Activity {
    EditText reg, Name, Pas1, Pas2, Email, Contact;
    String regid,emailid;
    SQLiteDatabase db;
    DatabaseHandler dbh = new DatabaseHandler(this);







    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_registration);
        reg = (EditText) findViewById(R.id.editText);
        Name = (EditText) findViewById(R.id.editText2);
        Pas1 = (EditText) findViewById(R.id.editText3);
        Pas2 = (EditText) findViewById(R.id.editText4);
        Email = (EditText) findViewById(R.id.editText5);
        Contact = (EditText) findViewById(R.id.editText6);
        Button btnReg = (Button) findViewById(R.id.btnRegister);

//button used to register

           btnReg.setOnClickListener(new View.OnClickListener() {


               @Override
               public void onClick(View v) {

                   dbh.open();
try {
    //get the id from database
    dbh.open();
    Cursor c=dbh.getRegid(reg.getText().toString().trim());
    if((c != null) && c.moveToFirst())
    {
        regid = c.getString(c.getColumnIndex("RegId"));
        c.close();
    }
    //get the email id from database
    Cursor email = dbh.getEmailid(Email.getText().toString().trim());
    if (email != null && email.moveToFirst()) {
        emailid = email.getString(email.getColumnIndex("Email"));
        email.close();
    }


    if (reg.getText().toString().equals("") || Name.getText().toString().equals("") || Pas1.getText().toString().equals("") || Pas2.getText().toString().equals("") || Email.getText().toString().equals("") || Contact.getText().toString().equals("")) {
        Toast.makeText(getApplicationContext(), "Fill All Mandatory Fields", Toast.LENGTH_SHORT).show();
    }
    else if (!Email.getText().toString().trim().matches("[a-zA-Z0-9._-]+@[a-z]+.[a-z]+"))
    {
       Email.setError("Invalid Email Address");
        Email.requestFocus();
    }
    else if (Email.getText().toString().equals(emailid))
    {
        Toast.makeText(getApplicationContext(), "Email is already registered", Toast.LENGTH_SHORT).show();
    }
    else if (Email.getText().toString().equals(regid))
    {
        Toast.makeText(getApplicationContext(), "Registration Id already exist", Toast.LENGTH_SHORT).show();
    }
    else if (!Contact.getText().toString().trim().matches("^[0-9]{10}$")) {
        Contact.setError("Invalid Contact Number");
        Contact.requestFocus();
    }
    else if (!Name.getText().toString().trim().matches("([a-zA-Z ]+)$")) {

        Name.setError("Invalid Name");
        Name.requestFocus();
    }
    else if (!Pas1.getText().toString().trim().matches(Pas2.getText().toString().trim())) {
        Pas2.setError("Password Not Matched");
        Pas2.requestFocus();
    }
    else {

        InsertRecord();
        Refresh();
        Intent i = new Intent(activity_registration.this, activity_home.class);
        startActivity(i);
        finish();
    }
}
catch (Exception e)
{
    String ex=e.toString();
    Toast.makeText(getApplicationContext(), ex, Toast.LENGTH_SHORT).show();
}
               }


              });
           }



    public  void InsertRecord()

    {
        String vQuery = "insert into tblReg (Registration,Name,Password,Email,Contact)values('" + reg.getText().toString().trim() + "','" + Name.getText().toString().trim() + "','"+ Pas2.getText().toString().trim()+"','"+ Email.getText().toString().trim()+"','"+ Contact.getText().toString().trim() +"')";
        DatabaseHandler db = new DatabaseHandler(this);
        db.InsertRecord(vQuery);
        Toast.makeText(getApplicationContext(), "Inserted successful", Toast.LENGTH_SHORT).show();
    }

    public void Refresh()
    {
        reg.setText("");
        Name.setText("");
        Pas1.setText("");
        Pas2.setText("");
        Email.setText("");
        Contact.setText("");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_registration, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

在您的表格中没有列RegId,因此请更改以下行

regid = c.getString(c.getColumnIndex("RegId"));

regid = c.getString(c.getColumnIndex("Registration"));

希望这会对你有所帮助。