繁体   English   中英

未能参加新活动

[英]Failing to get to a new activity

我是 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>

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

        }
    });

}

}

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

            }

        }
    });*/

}

}

从我在这里可以读到的所有内容中,我猜您在这一行中传递了错误的上下文:

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

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

而不是 startActivity(new Intent(getApplicationContext(), LoginPage.class));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM