Failing to get to a new activity

Viewed 83

I'm a newbie to AndroidStudio. I have a button on my register page that is supposed to lead to my login page but when I clicked on it, the same page popped out and the button that led to this becomes not clickable somehow. I checked and it says:

W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@17ab60d

Why did it start the same new activity but not my login activity? I think I might've got something messed up in my manifest file but I really have no idea what's going on here.

AndroidManifest.xml

<activity android:name=".AccountCreation">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".LoginPage"
        android:label="@string/login">
        <intent-filter>
            <action android:name="android.intent.action.ACTIVITY" />
        </intent-filter>
    </activity>

account_creation.java

public class AccountCreation extends AppCompatActivity {
//create all the variables
EditText input_email, input_password;
Button register;
TextView newLogin;
FirebaseAuth Auth;

//A method to move for the login button
void moveToLogin(){
    Intent intent = new Intent(AccountCreation.this, LoginPage.class);
    AccountCreation.this.startActivity(intent);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.account_creation);

    //Initialize all the views
    input_email = findViewById(R.id.input_email);
    input_password = findViewById(R.id.input_password);
    register = findViewById(R.id.register);
    Auth = FirebaseAuth.getInstance();
    newLogin = findViewById(R.id.newLogin);

    //Check to see if there is a user logged in now
    if (Auth.getCurrentUser() !=null ){
        startActivity(new Intent(getApplicationContext(),MainPage.class));
        finish();
    }

    //When LOGIN textView is clicked
    newLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            moveToLogin();
        }
    });

    //When REGISTER button is clicked
    register.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {

            //Get the string values of the user inputs
            String string_email = input_email.getText().toString().trim();
            String string_password = input_password.getText().toString().trim();

            //Check if the input fields are empty and password > 6
            if (TextUtils.isEmpty(string_email)){
                input_email.setError("Email is required.");
                return;
            }
            if (TextUtils.isEmpty(string_password)){
                input_password.setError("Password is required.");
                return;
            }
            if (string_password.length()<6){
                input_password.setError("Password must be longer than 6 characters");
                return;
            }

            //If all the conditions above is checked, account creation through firebase
            Auth.createUserWithEmailAndPassword(string_email,string_password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    //Check to see if the creation was successful
                    if (task.isSuccessful()){
                        Toast.makeText(AccountCreation.this, "Account created", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(getApplicationContext(), LoginPage.class));
                    }
                    //if the creation failed...
                    else {
                        Toast.makeText(AccountCreation.this, "Account creation failed " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            });

        }
    });

}

}

LoginPage.java

public class LoginPage extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.account_creation);
    EditText input_email, input_password;
    Button login;

    input_email = findViewById(R.id.input_email);
    input_password = findViewById(R.id.input_password);
    login = findViewById(R.id.login);
    //connect each one with the views

    String string_email = input_email.getText().toString();
    String string_password = input_password.getText().toString();
    //get the String values of the user inputs

    /*login.setOnClickListener(new View.OnClickListener() {
        //when login is clicked
        @Override
        public void onClick(View v) {

            //check to see if the inputs are empty
            if (string_email.isEmpty()) {
                Toast.makeText(LoginPage.this, "Please fill in the email and password for login", Toast.LENGTH_SHORT).show();
            } else if (string_password.isEmpty()) {
                Toast.makeText(LoginPage.this, "Please fill in the email and password for login", Toast.LENGTH_SHORT).show();
            } else {

            }

        }
    });*/

}

}

2 Answers

From all that I can read here I am guessing you are passing the wrong context in this line :

startActivity(new Intent(getApplicationContext(), LoginPage.class));

it should be

startActivity(new Intent(AccountCreation.this, LoginPage.class));

You are passing the ApplicationContext, where you need the AccountCreation context.

Logically this should also be changed :

if (Auth.getCurrentUser() !=null ){
    startActivity(new Intent(getApplicationContext(), MainPage.class));
    finish();
}

to :

if (Auth.getCurrentUser() !=null ){
    startActivity(new Intent(AccountCreation.this, MainPage.class));
    finish();
}

Write startActivity(new Intent (Account Creation.this, LoginPage.class));

Instead of startActivity(new Intent(getApplicationContext(), LoginPage.class));

Related