java—无法进入新活动

bqucvtff  于 2021-07-03  发布在  Java
关注(0)|答案(3)|浏览(273)

我是androidstudio的新手。我的注册页面上有一个按钮,应该指向我的登录页面,但当我点击它时,同一个页面弹出,导致这个按钮变得不可点击。我查了一下,上面写着: W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@17ab60d 为什么它启动了相同的新活动而不是我的登录活动?我想我可能在我的清单文件里搞砸了什么,但我真的不知道这是怎么回事。
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>

账户创建.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();
                    }
                }
            });

        }
    });

}

}
登录页.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 {

            }

        }
    });*/

}

}

ghhkc1vu

ghhkc1vu1#

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

根据您的意图尝试此代码。

4nkexdtk

4nkexdtk2#

从我在这里读到的所有内容来看,我猜你在这行中传递了错误的上下文:

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

应该是的

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

您正在传递applicationcontext,其中需要accountcreation上下文。
从逻辑上讲,这也应该改变:

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

收件人:

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

s6fujrry3#

write startactivity(新意图(account creation.this,loginpage.class));
而不是startactivity(new intent(getapplicationcontext(),loginpage.class));

相关问题