-2

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;
    }
}
Kaylee
  • 13
  • 5

2 Answers2

3

You have to add .show() at the end of toast to make it visible, So

Use this:

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();
 }

instead of this:

 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);
 }
Umair
  • 6,366
  • 15
  • 42
  • 50
  • Hi sir thanks for the answer but can i do it with intent? when i change the toast the app force close – Kaylee May 08 '19 at 06:35
  • @Kaylee what do you want to do with intent ? It's a different thing then a toast. – Umair May 08 '19 at 06:41
  • @Kaylee and if your app force closes then there must be something in your logs. Can you share that too ? – Umair May 08 '19 at 06:41
  • Sir i wanted my it to intent to another class when its done logging in but i dont know how if(res==true){ T Intent mainIntent = new Intent(MainActivity.this, MainPage.class); startActivity(mainIntent); } – Kaylee May 08 '19 at 07:09
  • @Kaylee you need to check this answer. This is how you will be able to go to next activity using intent. https://stackoverflow.com/questions/4186021/how-to-start-new-activity-on-button-click – Umair May 08 '19 at 07:13
1

Use this:

Toast.makeText(MainActivity.this,"You are logged in",Toast.LENGTH_SHORT).show();

Instead of:

Toast.makeText(MainActivity.this,"You are logged in",Toast.LENGTH_SHORT);
Dinesh Shingadiya
  • 988
  • 1
  • 8
  • 23