我有一个问题,当我单击登录按钮时,Toast make文本不会显示,并且我不知道这是怎么回事,因为
我看不到任何错误,当我将toast.maketext更改为Intent时,应用程序强制关闭。希望有人能帮忙。
我看不到任何错误,当我将toast.maketext更改为Intent时,应用程序强制关闭。希望有人能帮忙。
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;
}
}
答案 0 :(得分:4)
您必须在吐司末添加.show()
,使其成为visible
,所以
使用此:
if(res==true){
Toast.makeText(MainActivity.this,"You are logged in",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this,"Please Enter Again",Toast.LENGTH_SHORT).show();
}
代替此:
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);
}
答案 1 :(得分:1)
使用此:
Toast.makeText(MainActivity.this,"You are logged in",Toast.LENGTH_SHORT).show();
代替:
Toast.makeText(MainActivity.this,"You are logged in",Toast.LENGTH_SHORT);