I have this problem that when I click my login button the Toast make text doesn't show up and I don't know what's wrong because
I can't see any error and when I change the toast.maketext to an Intent the app force closes. Hope someone can help.
I can't see any error and when I change the toast.maketext to an Intent the app force closes. Hope someone can help.
MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText email;
EditText password;
Button login;
Button signup;
DBHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DBHelper(this);
email = findViewById(R.id.editText_email);
password = findViewById(R.id.editText_password);
login = findViewById(R.id.btnlogin);
signup = findViewById(R.id.btnsignup);
signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent signupIntent = new Intent(MainActivity.this, SignUp.class);
startActivity(signupIntent);
}
});
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String user = email.getText().toString();
String pass = password.getText().toString();
Boolean res = db.checkAcc(user,pass);
if(res==true){
Toast.makeText(MainActivity.this,"You are logged in",Toast.LENGTH_SHORT);
}
else{
Toast.makeText(MainActivity.this,"Please Enter Again",Toast.LENGTH_SHORT);
}
}
});
}
}
DBHelper.java
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME="register.db";
public static final String TABLE_NAME="register";
public static final String COL_1="ID";
public static final String COL_2="email";
public static final String COL_3="password";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("CREATE TABLE register (ID INTEGER PRIMARY KEY AUTOINCREMENT,email TEXT UNIQUE, password TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqLiteDatabase);
}
public long AddAcc(String email, String password){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("email",email);
contentValues.put("password",password);
long res = db.insert("register",null,contentValues);
db.close();
return res;
}
public boolean checkAcc(String username,String password){
String[] columns = { COL_1 };
SQLiteDatabase db = getReadableDatabase();
String selection = COL_2 + "=?" + " and " + COL_3 + "=?";
String[] selectionArgs = {username,password};
Cursor cursor = db.query(TABLE_NAME,columns,selection,selectionArgs,null,null,null);
int count = cursor.getCount();
cursor.close();
db.close();
if(count>0)
return true;
else
return false;
}
}