当我点击登录按钮时,它会显示错误"不幸的是应用已停止",那么我可以对登录页面以及数据库文件进行哪些更改?
这是我的Login.java文件
package com.example.panicapp;
import com.example.panicapp.AppDao.AuthenticationDao;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Login extends Activity implements OnClickListener{
private Button btnRegister;
private Button btnLogin;
private Button btnAbout;
private Button btnExit;
private TextView txv;
private EditText edtemail_login;
private EditText edtpassword_login;
private AuthenticationDao authdao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
authdao=new AuthenticationDao(Login.this);
getId();
regId();
edtemail_login.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
Is_Valid_email(edtemail_login); // pass your EditText Obj here.
}
});
}
public void Is_Valid_email(EditText edtemail_login)
{
if (edtemail_login.getText().toString() == null) {
edtemail_login.setError("Invalid Email Address");
} else if (isEmailValid(edtemail_login.getText().toString()) == false) {
edtemail_login.setError("Invalid Email Address");
} else {
edtemail_login.getText().toString();
}
}
private boolean isEmailValid(String string) {
// TODO Auto-generated method stub
return android.util.Patterns.EMAIL_ADDRESS.matcher(string).matches();
}
private void getId()
{
try
{
btnRegister=(Button)findViewById(R.id.btn_register);
btnLogin=(Button)findViewById(R.id.btn_login);
btnAbout=(Button)findViewById(R.id.btn_about_login);
btnExit=(Button)findViewById(R.id.btn_exit_login);
edtemail_login=(EditText)findViewById(R.id.edt_email_login);
edtpassword_login=(EditText)findViewById(R.id.edt_password_login);
txv=(TextView)findViewById(R.id.txv_login);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void regId()
{
try
{
btnRegister.setOnClickListener(this);
btnLogin.setOnClickListener(this);
btnAbout.setOnClickListener(this);
btnExit.setOnClickListener(this);
txv.setOnClickListener(this);
}
catch(Exception e)
{
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.equals(btnRegister))
{
Intent i= new Intent(Login.this, Registration.class);
startActivity(i);
}
else if(v.equals(btnLogin))
{
edtemail_login=(EditText)findViewById(R.id.edt_email_login);
edtpassword_login=(EditText)findViewById(R.id.edt_password_login);
String email=edtemail_login.getText().toString();
String password=edtpassword_login.getText().toString();
Log.i("email and pass is", "email:"+email+"pass:"+password);
authdao.validateLogin(email, password);
Log.i("validation", ""+authdao.validateLogin(email, password));
Intent i= new Intent(Login.this, HomePage.class);
startActivity(i);
}
else if(v.equals(btnAbout))
{
Intent i= new Intent(Login.this, About.class);
startActivity(i);
}
else if(v.equals(btnExit))
{
finish();
System.exit(0);
}
if(v.equals(txv))
{
Intent i=new Intent(Login.this, ForgotPassword.class);
startActivity(i);
}
}
}
这是数据库类
package com.example.panicapp.AppDao;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.example.panicapp.AppModel.AddEmergencyNumber;
import com.example.panicapp.AppModel.AddPanicVO;
import com.example.panicapp.AppModel.LoginVO;
import com.example.panicapp.AppModel.PersonalInformationVO;
import com.example.panicapp.AppUtill.DbUtill;
public class AuthenticationDao extends SQLiteOpenHelper implements OnAuthenticationListener{
private final String LOGTAG="AuthenticationDao";
private String CREATE_LOGIN_MASTER="";
private String CREATE_ALTERNATIVE_METHODS="";
private String CREATE_SMS_MASTER="";
private String CREATE_CALL_MASTER="";
private String CREATE_ADD_PANIC="";
private String CREATE_USER_LOCATION="";
private String CREATE_PERSONAL_INFORMATION="";
private String CREATE_ADD_EMERGENCY_NUMBER="";
private static SQLiteDatabase sqlDb=null;
/*public AuthenticationDao(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}*/
public AuthenticationDao(Context context)
{
super(context, DbUtill.DATABASE_NAME, null, DbUtill.DATABASE_VERSION);
Log.i("AuthenticationDao", "This is AuthenticationDao constructor.....");
sqlDb=this.getWritableDatabase();
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db)
{
// TODO Auto-generated method stub
CREATE_LOGIN_MASTER = "CREATE TABLE " + DbUtill.TABLE_LOGIN_MASTER + "("
+ DbUtill.USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ DbUtill.EMAIL + " TEXT not null,"
+ DbUtill.PASSWORD + " TEXT not null" + ")";
db.execSQL(CREATE_LOGIN_MASTER);
Log.i(LOGTAG, "CREATE_LOGIN_MASTER :" +CREATE_LOGIN_MASTER);
CREATE_PERSONAL_INFORMATION = "CREATE TABLE " + DbUtill.TABLE_PERSONAL_INFORMATION + "("
+ DbUtill.USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ DbUtill.FIRST_NAME + " TEXT NOT NULL,"
+ DbUtill.LAST_NAME + " TEXT NOT NULL,"
+ DbUtill.PHONE + " INTEGER NOT NULL,"
+ DbUtill.ADDRESS + " TEXT NOT NULL,"
+ DbUtill.COUNTRY + " TEXT NOT NULL,"
+ DbUtill.STATE + " TEXT NOT NULL,"
+ DbUtill.CITY + " Text NOT NULL,"
+ " FOREIGN KEY ("+DbUtill.USER_ID+") REFERENCES "+DbUtill.TABLE_LOGIN_MASTER+"("+DbUtill.USER_ID+")" + ")";
db.execSQL(CREATE_PERSONAL_INFORMATION);
Log.i(LOGTAG, "CREATE_PERSONAL_INFORMATION :" +CREATE_PERSONAL_INFORMATION);
CREATE_ALTERNATIVE_METHODS= " CREATE TABLE " + DbUtill.TABLE_ALTERNATIVE_METHODS + "("
+ DbUtill.METHODS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ DbUtill.TITLE + " TEXT NOT NULL,"
+ DbUtill.PROCEDURE + " TEXT NOT NULL" + ")";
db.execSQL(CREATE_ALTERNATIVE_METHODS);
Log.i(LOGTAG, "CREATE_ALTERNATIVE_METHODS :" +CREATE_ALTERNATIVE_METHODS);
CREATE_SMS_MASTER= " CREATE TABLE " + DbUtill.TABLE_SMS_MASTER + "("
+ DbUtill.USER_ID + " INTEGER NOT NULL,"
+ DbUtill.SMS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ DbUtill.TIME_STAMP_SMS + " DATETIME NOT NULL,"
+ " FOREIGN KEY ("+DbUtill.USER_ID+") REFERENCES "+DbUtill.TABLE_LOGIN_MASTER+"("+DbUtill.USER_ID+")" + ")";
db.execSQL(CREATE_SMS_MASTER);
Log.i(LOGTAG, "CREATE_SMS_MASTER :" +CREATE_SMS_MASTER);
CREATE_CALL_MASTER= " CREATE TABLE " + DbUtill.TABLE_CALL_MASTER + "("
+ DbUtill.USER_ID + " INTEGER NOT NULL,"
+ DbUtill.CALL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ DbUtill.TIME_STAMP_CALL + " DATETIME NOT NULL,"
+ " FOREIGN KEY ("+DbUtill.USER_ID+") REFERENCES "+DbUtill.TABLE_LOGIN_MASTER+"("+DbUtill.USER_ID+")" + ")";
db.execSQL(CREATE_CALL_MASTER);
Log.i(LOGTAG, "CREATE_CALL_MASTER :" +CREATE_CALL_MASTER);
CREATE_USER_LOCATION= " CREATE TABLE " + DbUtill.TABLE_USER_LOCATION + "("
+ DbUtill.USER_ID + " INTEGER NOT NULL,"
+ DbUtill.LOCATION_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ DbUtill.LOCATION + " TEXT NOT NULL,"
+ DbUtill.TIME_STAMP_LOCATION + " DATETIME NOT NULL,"
+ " FOREIGN KEY ("+DbUtill.USER_ID+") REFERENCES "+DbUtill.TABLE_LOGIN_MASTER+"("+DbUtill.USER_ID+")" + ")";
db.execSQL(CREATE_USER_LOCATION);
Log.i(LOGTAG, "CREATE_USER_LOCATION :" +CREATE_USER_LOCATION);
CREATE_ADD_PANIC= " CREATE TABLE " + DbUtill.TABLE_ADD_PANIC + "("
+ DbUtill.PANIC_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ DbUtill.TITLE + " TEXT NOT NULL,"
+ DbUtill.PROCEDURE + " TEXT NOT NULL,"
+ DbUtill.SUGGESTED_USER_NAME + " TEXT NOT NULL,"
+ DbUtill.SUGGESTED_USER_CONTACT + " INTEGER NOT NULL" + ")";
db.execSQL(CREATE_ADD_PANIC);
Log.i(LOGTAG, "CREATE_ADD_PANIC :" +CREATE_ADD_PANIC);
CREATE_ADD_EMERGENCY_NUMBER= " CREATE TABLE " + DbUtill.TABLE_ADD_EMERGENCY_NUMBER + "("
+ DbUtill.USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ DbUtill.EMERGENCYNUMBER + " INTEGER NOT NULL,"
+ DbUtill.CALL + " INTEGER NOT NULL,"
+ DbUtill.GPS + " INTEGER NOT NULL,"
+ DbUtill.SMS + " INTEGER NOT NULL,"
+ " FOREIGN KEY ("+DbUtill.USER_ID+") REFERENCES "+DbUtill.TABLE_LOGIN_MASTER+"("+DbUtill.USER_ID+")" + ")";
db.execSQL(CREATE_ADD_EMERGENCY_NUMBER);
Log.i(LOGTAG, "CREATE_ADD_EMERGENCY_NUMBER :" +CREATE_ADD_EMERGENCY_NUMBER);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXIST" +DbUtill.TABLE_LOGIN_MASTER);
db.execSQL("DROP TABLE IF EXIST" +DbUtill.TABLE_PERSONAL_INFORMATION);
db.execSQL("DROP TABLE IF EXIST" +DbUtill.TABLE_ALTERNATIVE_METHODS);
db.execSQL("DROP TABLE IF EXIST" +DbUtill.TABLE_USER_LOCATION);
db.execSQL("DROP TABLE IF EXIST" +DbUtill.TABLE_SMS_MASTER);
db.execSQL("DROP TABLE IF EXIST" +DbUtill.TABLE_CALL_MASTER);
db.execSQL("DROP TABLE IF EXIST" +DbUtill.TABLE_ADD_EMERGENCY_NUMBER);
db.execSQL("DROP TABLE IF EXIST" +DbUtill.TABLE_ADD_PANIC);
}
@Override
public void openDatabase()
{
// TODO Auto-generated method stub
sqlDb=this.getReadableDatabase();
}
@Override
public void closeDatabase()
{
// TODO Auto-generated method stub
sqlDb.close();
}
public void regUser(LoginVO logvo, PersonalInformationVO pervo)
{
// TODO Auto-generated method stub
try
{
sqlDb = this.getWritableDatabase();
ContentValues conv=new ContentValues();
conv.put(DbUtill.EMAIL, logvo.getEmailId());
conv.put(DbUtill.PASSWORD, logvo.getPassword());
long insertId=sqlDb.insert(DbUtill.TABLE_LOGIN_MASTER, null, conv);
Log.i("AuthentictionDao", "New Raw Inserted...."+insertId);
ContentValues conv1=new ContentValues();
conv1.put(DbUtill.USER_ID, insertId);
conv1.put(DbUtill.FIRST_NAME, pervo.getfName());
conv1.put(DbUtill.LAST_NAME, pervo.getLastName());
conv1.put(DbUtill.PHONE, pervo.getContact());
conv1.put(DbUtill.ADDRESS, pervo.getAddress());
conv1.put(DbUtill.COUNTRY, pervo.getCountry());
conv1.put(DbUtill.STATE, pervo.getState());
conv1.put(DbUtill.CITY, pervo.getCity());
long insertId1=sqlDb.insert(DbUtill.TABLE_PERSONAL_INFORMATION, null, conv1);
Log.i("AuthentictionDao", "New Raw Inserted into Personal Information...."+insertId1);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void addPanic(AddPanicVO addvo)
{
// TODO Auto-generated method stub
try
{
sqlDb = this.getWritableDatabase();
ContentValues conv=new ContentValues();
conv.put(DbUtill.TITLE, addvo.getTitle());
conv.put(DbUtill.PROCEDURE, addvo.getProcedure());
conv.put(DbUtill.SUGGESTED_USER_NAME, addvo.getSuggestedUserName());
conv.put(DbUtill.SUGGESTED_USER_CONTACT, addvo.getSuggestedUserContact());
long insertId=sqlDb.insert(DbUtill.TABLE_ADD_PANIC, null, conv);
Log.i("AuthentictionDao", "New Panic Inserted...."+insertId);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void addNumber(AddEmergencyNumber numcall)
{
// TODO Auto-generated method stub
try
{
sqlDb = this.getWritableDatabase();
ContentValues conv=new ContentValues();
conv.put(DbUtill.USER_ID, numcall.getUserId());
conv.put(DbUtill.EMERGENCYNUMBER, numcall.getEmergencyNumber());
conv.put(DbUtill.CALL, numcall.getCall());
conv.put(DbUtill.SMS, numcall.getSms());
conv.put(DbUtill.GPS, numcall.getGps());
long insertId=sqlDb.insert(DbUtill.TABLE_ADD_EMERGENCY_NUMBER, null, conv);
Log.i("AuthentictionDao", "New Emergency Number Inserted...."+insertId);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void addNumber1(AddEmergencyNumber numsms) {
// TODO Auto-generated method stub
try
{
sqlDb = this.getWritableDatabase();
ContentValues conv=new ContentValues();
conv.put(DbUtill.USER_ID, numsms.getUserId());
conv.put(DbUtill.EMERGENCYNUMBER, numsms.getEmergencyNumber());
conv.put(DbUtill.CALL, numsms.getCall());
conv.put(DbUtill.SMS, numsms.getSms());
conv.put(DbUtill.GPS, numsms.getGps());
long insertId=sqlDb.insert(DbUtill.TABLE_ADD_EMERGENCY_NUMBER, null, conv);
Log.i("AuthentictionDao", "New Emergency Number Inserted...."+insertId);
}
catch(Exception e)
{
e.printStackTrace();
}
}
@Override
public void addNumberForGPS(AddEmergencyNumber numgps)
{
// TODO Auto-generated method stub
try
{
sqlDb = this.getWritableDatabase();
ContentValues conv=new ContentValues();
conv.put(DbUtill.USER_ID, numgps.getUserId());
conv.put(DbUtill.EMERGENCYNUMBER, numgps.getEmergencyNumber());
conv.put(DbUtill.CALL, numgps.getCall());
conv.put(DbUtill.SMS, numgps.getSms());
conv.put(DbUtill.GPS, numgps.getGps());
long insertId=sqlDb.insert(DbUtill.TABLE_ADD_EMERGENCY_NUMBER, null, conv);
Log.i("AuthentictionDao", "New Emergency Number Inserted...."+insertId);
}
catch(Exception e)
{
e.printStackTrace();
}
}
@Override
public void showPassword()
{
// TODO Auto-generated method stub
try
{
sqlDb=this.getReadableDatabase();
String query="SELECT "+DbUtill.PASSWORD+" FROM "+DbUtill.TABLE_PERSONAL_INFORMATION+" WHERE "+DbUtill.EMAIL+"=?";
sqlDb.rawQuery(query, null);
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
sqlDb.close();
}
}
@Override
public boolean validateLogin(String email, String password)
{
sqlDb=this.getWritableDatabase();
boolean checklogin=false;
Cursor cursor=null;
/* Cursor myCursor = db.query(DATABASE_SELECT_USERS,
new String[] { USER_ID, USER_NAME, USER_PASSWORD },
USER_NAME + "='" + username + "' AND " +
USER_PASSWORD + "='" + password + "'", null, null, null, null);*/
cursor=sqlDb.query(DbUtill.TABLE_LOGIN_MASTER, new String[] {DbUtill.USER_ID,DbUtill.EMAIL,DbUtill.PASSWORD}, DbUtill.EMAIL+""+email +"AND"+DbUtill.PASSWORD+""+password+"", null, null, null, null);
if(cursor==null)
{
//set boolean var to false
checklogin=false;
//message no such records
}
else
{
//set boolean var to true
checklogin=true;
cursor.moveToFirst();
}
return checklogin;
// TODO Auto-generated method stub
}
}
有人可以建议验证代码吗?
这是我得到的日志猫错误。
04-27 22:02:36.546: I/AuthenticationDao(24430): This is AuthenticationDao constructor.....
04-27 22:02:46.101: I/email and pass is(24430): email:pass:
04-27 22:02:46.101: E/SQLiteLog(24430): (1) no such column: email_idANDpassword
04-27 22:02:46.101: D/AndroidRuntime(24430): Shutting down VM
04-27 22:02:46.101: W/dalvikvm(24430): threadid=1: thread exiting with uncaught exception (group=0x41aaa700)
04-27 22:02:46.106: E/AndroidRuntime(24430): FATAL EXCEPTION: main
04-27 22:02:46.106: E/AndroidRuntime(24430): android.database.sqlite.SQLiteException: no such column: email_idANDpassword (code 1): , while compiling: SELECT user_id, email_id, password FROM login_master_table WHERE email_idANDpassword
04-27 22:02:46.106: E/AndroidRuntime(24430): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
04-27 22:02:46.106: E/AndroidRuntime(24430): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1118)
04-27 22:02:46.106: E/AndroidRuntime(24430): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:691)
04-27 22:02:46.106: E/AndroidRuntime(24430): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
04-27 22:02:46.106: E/AndroidRuntime(24430): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
04-27 22:02:46.106: E/AndroidRuntime(24430): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
04-27 22:02:46.106: E/AndroidRuntime(24430): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
04-27 22:02:46.106: E/AndroidRuntime(24430): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1436)
04-27 22:02:46.106: E/AndroidRuntime(24430): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1283)
04-27 22:02:46.106: E/AndroidRuntime(24430): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1154)
04-27 22:02:46.106: E/AndroidRuntime(24430): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1322)
04-27 22:02:46.106: E/AndroidRuntime(24430): at com.example.panicapp.AppDao.AuthenticationDao.validateLogin(AuthenticationDao.java:299)
04-27 22:02:46.106: E/AndroidRuntime(24430): at com.example.panicapp.Login.onClick(Login.java:129)
04-27 22:02:46.106: E/AndroidRuntime(24430): at android.view.View.performClick(View.java:4475)
04-27 22:02:46.106: E/AndroidRuntime(24430): at android.view.View$PerformClick.run(View.java:18786)
04-27 22:02:46.106: E/AndroidRuntime(24430): at android.os.Handler.handleCallback(Handler.java:730)
04-27 22:02:46.106: E/AndroidRuntime(24430): at android.os.Handler.dispatchMessage(Handler.java:92)
04-27 22:02:46.106: E/AndroidRuntime(24430): at android.os.Looper.loop(Looper.java:137)
04-27 22:02:46.106: E/AndroidRuntime(24430): at android.app.ActivityThread.main(ActivityThread.java:5493)
04-27 22:02:46.106: E/AndroidRuntime(24430): at java.lang.reflect.Method.invokeNative(Native Method)
04-27 22:02:46.106: E/AndroidRuntime(24430): at java.lang.reflect.Method.invoke(Method.java:525)
04-27 22:02:46.106: E/AndroidRuntime(24430): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
04-27 22:02:46.106: E/AndroidRuntime(24430): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
04-27 22:02:46.106: E/AndroidRuntime(24430): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
logcat说明了一切。在创建查询的行上,您忘记在列名和单词之间放置一个空格。 SQL正在尝试查找名为emailandpassword的列,而不是电子邮件和密码。 问题是,为什么要在本地数据库中查询密码?这不是很安全。