我必须使用 SQLite 数据库在我的 Android 应用程序中实现登录应用程序。首先,我创建了注册数据库,并在表中插入了值,我已经完成了这一点。当我首先运行我的应用程序时,我成功注册并将数据插入数据库,然后我有登录数据没有得到它显示错误 NullPointerException .这是我的代码。
public class DataBaseHandller extends SQLiteOpenHelper
{
public DataBaseHandller(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "LeadManagment";
// table's name
private static final String TABLE_REGISTER = "registration";
// Register Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static String KEY_EMAIL = "email";
private static final String KEY_PIN = "pin";
@Override
public void onCreate(SQLiteDatabase db)
{
String CREATE_REGISTER_TABLE = "CREATE TABLE " + TABLE_REGISTER + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_EMAIL + " TEXT," + KEY_PIN + " TEXT" + ")";
db.execSQL(CREATE_REGISTER_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_REGISTER);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new contact Or Insert Field In TableS
public void Add_Contact(Register_BeanClass register_Bean) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues insert_ContentValues = new ContentValues();
insert_ContentValues.put(KEY_NAME, register_Bean.get_name());
insert_ContentValues.put(KEY_EMAIL, register_Bean.get_email());
insert_ContentValues.put(KEY_PIN, register_Bean.get_pin());
// Inserting Row
db.insert(TABLE_REGISTER, null, insert_ContentValues);
db.close(); // Closing database connection
}
//Get Single Antry
public String getSinlgeEntry(String userName)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor=db.query(TABLE_REGISTER, null, KEY_EMAIL +="?", new String[]{userName}, null, null, null);
if(cursor.getCount()<1) // UserName Not Exist
{
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String password= cursor.getString(cursor.getColumnIndex(KEY_PIN));
cursor.close();
return password;
}
// Getting All Contacts
public ArrayList<Register_BeanClass> Get_Contacts()
{
try
{
register_list.clear();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_REGISTER;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst())
{
do
{
Register_BeanClass contact = new Register_BeanClass();
contact.setId(Integer.parseInt(cursor.getString(0)));
contact.set_name(cursor.getString(1));
contact.set_email(cursor.getString(2));
contact.set_pin(cursor.getString(3));
register_list.add(contact);
} while (cursor.moveToNext());
}
// return contact list
cursor.close();
db.close();
return register_list;
}
catch (Exception e)
{
// TODO: handle exception
Log.e("all_contact", "" + e);
}
return register_list;
}
// Updating single contact
public int Update_RegisterField(Register_BeanClass update_Register_BeanClass) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues updateContentvalues = new ContentValues();
updateContentvalues.put(KEY_NAME, update_Register_BeanClass.get_name());
updateContentvalues.put(KEY_EMAIL, update_Register_BeanClass.get_email());
updateContentvalues.put(KEY_PIN, update_Register_BeanClass.get_pin());
// updating row
return db.update(TABLE_REGISTER, updateContentvalues, KEY_ID + " = ?",
new String[] { String.valueOf(update_Register_BeanClass.getId()) });
}
// Deleting single contact
public void Delete_Contact(int id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_REGISTER, KEY_ID + " = ?",
new String[] { String.valueOf(id) });
db.close();
}
// Getting contacts Count
public int Get_Total_Contacts() {
String countQuery = "SELECT * FROM " + TABLE_REGISTER;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
}
public class Registration_Activity1 extends Activity
{
TextView eText_TotalRecord;
EditText editTextUserName,editTextEmailId , editTextPin,editTextConfirmPin;
Button btnCreateAccount;
Button btnViewDetails;
DataBaseHandller dbHandller=new DataBaseHandller(this);
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.registration_activity1);
editTextUserName = (EditText) findViewById(R.id.editText_regName);
editTextEmailId = (EditText) findViewById(R.id.editText_regEmail);
editTextPin = (EditText) findViewById(R.id.editText_regApp_Pin);
editTextConfirmPin = (EditText) findViewById(R.id.editText_regConfirm_App_Pin);
eText_TotalRecord=(TextView)findViewById(R.id.etext_totalRecord);
btnCreateAccount=(Button)findViewById(R.id.reg_btnSubmit);
btnCreateAccount.setOnClickListener(new OnClickListener() {
@SuppressWarnings("unused")
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String userName=editTextUserName.getText().toString();
String userEmail=editTextEmailId.getText().toString();
String userPin=editTextPin.getText().toString();
String userConfirmPin=editTextConfirmPin.getText().toString();
// check if any of the fields are vaccant
if(userName.equals("")||userEmail.equals("")||userPin.equals("")||userConfirmPin.equals(""))
{
Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show();
return;
}
// check if both password matches
if(!userPin.equals(userConfirmPin))
{
Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_LONG).show();
return;
}
else
{
// Save the Data in Database
dbHandller.Add_Contact(new Register_BeanClass(0, userName,userEmail,userPin));
Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
}
}
});
btnLogin=(Button) findViewById(R.id.btn_Login);
btnLogin.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0) {
// TODO Auto-generated method stub
str_EmailId=editText_Email.getText().toString().trim();
str_AppPin=editText_App_Pin.getText().toString().trim();
// fetch the Password form database for respective user name
String storedApplication=db.getSinlgeEntry(str_AppPin);
if(str_AppPin.equals(storedApplication))
{
Toast.makeText(Application_Pin_Activity2.this,"Access Allow", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(Application_Pin_Activity2.this, "Sorry ! Wrong Pin", Toast.LENGTH_LONG).show();
}
}
});
}