Android Studio button going in to wrong page

S.Levo :

In my main page I have 3 different buttons.

  1. Login (loginB)
  2. Create a new account (createB)
  3. Terms of conduct (termsB)

So far it has worked just fine, but for some reason I have now faced a problem with the create new account button. It did go to the right page before but now it goes to a wrong page. It should be going to the CreateAccount -page but it is going to the Menu -page.

I have the java code in the MainActivity were the buttons are here:

public class MainActivity extends AppCompatActivity {
    Button loginB;
    Button termsB;
    Button createB;
    EditText emailL,passwordL;
    FirebaseAuth fAuth;
    ProgressBar progressB;

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

        createB = findViewById(R.id.createB);
        createB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openCreateAccount();
            }
        });

        termsB = findViewById(R.id.termsB);
        termsB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openTermsOfConduct();
            }
        });

        emailL = findViewById(R.id.emailL);
        passwordL = findViewById(R.id.passwordL);
        fAuth = FirebaseAuth.getInstance();
        loginB = findViewById(R.id.loginB);
        progressB = findViewById(R.id.progressB);

        loginB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = emailL.getText().toString().trim();
                String password = passwordL.getText().toString().trim();

                if(TextUtils.isEmpty(email)){
                    emailL.setError("Email is required.");
                    return;
                }

                if(TextUtils.isEmpty(password)){
                    passwordL.setError("Password is required.");
                    return;
                }

                if(password.length() <6){
                    passwordL.setError("Password must be at least 6 characters.");
                }

                progressB.setVisibility(View.VISIBLE);

                //authenticate the user

                fAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(task.isSuccessful()){
                            Toast.makeText(MainActivity.this, "Logged in successfully", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(getApplicationContext(),Menu.class));
                        }
                        else{
                            Toast.makeText(MainActivity.this, "Error " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                            progressB.setVisibility(View.INVISIBLE);
                        }
                    }
                });

            }
        });
    }

    public void openCreateAccount(){
        Intent intent = new Intent(this, CreateAccount.class);
        startActivity(intent);
    }

    public void openTermsOfConduct(){
        Intent intent = new Intent(this, TermsOfConduct.class);
        startActivity(intent);
    }
}

Main Activity xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    android:background="@drawable/tausta">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:fontFamily="sans-serif"
        android:paddingVertical="30dp"
        android:text="Login"
        android:textAlignment="center"
        android:textColor="#000"
        android:textSize="90sp"
        android:layout_marginBottom="25dp"
        />

    <EditText
        android:id="@+id/emailL"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="30dp"
        android:layout_marginBottom="30dp"
        android:textColor="#fff"
        android:textSize="25sp"
        android:hint="Email"
        android:textColorHint="#fff"
        />

    <EditText
        android:id="@+id/passwordL"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="30dp"
        android:inputType="textPassword"
        android:layout_marginBottom="30dp"
        android:textColor="#fff"
        android:textSize="25sp"
        android:hint="Password"
        android:textColorHint="#fff"
        />

    <Button
        android:id="@+id/loginB"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Login"
        android:layout_marginHorizontal="30dp"
        android:layout_marginBottom="25dp"
        android:background="@drawable/button_bg"
        />

    <Button
        android:id="@+id/createB"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Create a new account"
        android:layout_marginHorizontal="30dp"
        android:layout_marginBottom="25dp"
        android:background="@drawable/button_bg"
        />

    <Button
        android:id="@+id/termsB"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Terms and conditions"
        android:layout_marginHorizontal="30dp"
        android:textColor="#0070ff"
        android:textAlignment="center"
        android:background="@android:color/transparent"
        />

    <ProgressBar
        android:id="@+id/progressB"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="invisible"
        />


</LinearLayout>

CreateAccount java:

public class CreateAccount extends AppCompatActivity {
    Button registerB;
    EditText nameR,emailR,pnumberR,passwordR;
    FirebaseAuth fAuth;
    ProgressBar progressB;



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

        nameR = findViewById(R.id.nameR);
        emailR = findViewById(R.id.emailR);
        pnumberR = findViewById(R.id.pnumberR);
        passwordR = findViewById(R.id.passwordR);
        progressB = findViewById(R.id.progressB);
        fAuth = FirebaseAuth.getInstance();

        //check if user registered

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


        registerB = findViewById(R.id.registerB);
        registerB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = emailR.getText().toString().trim();
                String password = passwordR.getText().toString().trim();

                if(TextUtils.isEmpty(email)){
                    emailR.setError("Email is required.");
                    return;
                }

                if(TextUtils.isEmpty(password)){
                    passwordR.setError("Password is required.");
                    return;
                }

                if(password.length() <6){
                    passwordR.setError("Password must be ata least 6 characters.");
                    return;
                }

                progressB.setVisibility(View.VISIBLE);

                //register the user to FireBase

                fAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(task.isSuccessful()){
                            Toast.makeText(CreateAccount.this, "User created.", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(getApplicationContext(),Menu.class));
                        }
                        else{
                            Toast.makeText(CreateAccount.this, "Error " +task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
    }
}

CreateAccount xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".CreateAccount"
    android:background="@drawable/tausta"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:fontFamily="sans-serif"
        android:paddingVertical="20dp"
        android:text="Create new account"
        android:textAlignment="center"
        android:textColor="#000"
        android:textSize="40sp"
        android:layout_marginBottom="25dp"
        />

    <EditText
        android:id="@+id/nameR"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Full name"
        android:textColorHint="#fff"
        android:textSize="25sp"
        android:layout_marginHorizontal="25dp"
        android:layout_marginBottom="30dp"
        />

    <EditText
        android:id="@+id/emailR"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Email"
        android:textColorHint="#fff"
        android:textSize="25sp"
        android:layout_marginHorizontal="25dp"
        android:layout_marginBottom="30dp"
        />

    <EditText
        android:id="@+id/pnumberR"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Phone number"
        android:textColorHint="#fff"
        android:textSize="25sp"
        android:layout_marginHorizontal="25dp"
        android:layout_marginBottom="30dp"
        />


    <EditText
        android:id="@+id/passwordR"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:textColorHint="#fff"
        android:textSize="25sp"
        android:layout_marginHorizontal="25dp"
        android:layout_marginBottom="30dp"
        />

    <Button
        android:id="@+id/registerB"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Register"
        android:layout_marginHorizontal="30dp"
        android:layout_marginBottom="25dp"
        android:background="@drawable/button_bg"
        />

    <ProgressBar
        android:id="@+id/progressB"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="invisible"
        />

</LinearLayout>
kyrax :

The following code might be the cause:

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

Can you check if it's going into the if block?

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=28877&siteId=1