无法打开另一个活动,单击按钮时会打开相同的活动

发布于 2025-01-11 22:27:55 字数 8716 浏览 0 评论 0原文

当我单击“创建帐户”按钮时,它应该打开另一个活动,但它只是再次打开相同的活动并删除我在电子邮件和密码中的输入。我还在清单中定义了我的活动。

package com.example.music_link_firebase;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

public class LoginActivity extends AppCompatActivity {

EditText lemail, lpassword;
Button login, lregister;
ProgressBar lprogressBar;
FirebaseAuth lfAuth;


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

    lemail = findViewById(R.id.email1);
    lpassword = findViewById(R.id.password1);
    login = findViewById(R.id.login1);
    lregister = findViewById(R.id.signup);
    lprogressBar = findViewById(R.id.progressBar2);
    lfAuth = FirebaseAuth.getInstance();

    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String email = lemail.getText().toString().trim();
            String password = lpassword.getText().toString().trim();

            if(TextUtils.isEmpty(email)){
                lemail.setError("Email is required");
                return;
            }
            if(TextUtils.isEmpty(password)){
                lpassword.setError("password is required");
            }
            if(password.length() < 6){
                lpassword.setError("password must be longer than 5 characters");
            }

            lprogressBar.setVisibility(View.VISIBLE);

            //authenticate the user

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

        }
    });

    lregister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(getApplicationContext(), MainActivity.class));
        }
    });




}

单击

按钮时应该打开的活动的 java 代码

package com.example.music_link_firebase;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

public class MainActivity extends AppCompatActivity {

    EditText rEmail, rpassword, rphone, rfullname;
    Button rsignin, rRegister;
    FirebaseAuth rfAuth;
    ProgressBar rprogressBar;

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

        rfullname = findViewById(R.id.fullname);
        rEmail = findViewById(R.id.email);
        rpassword = findViewById(R.id.password);
        rphone = findViewById(R.id.phone);
        rsignin = findViewById(R.id.signin);
        rRegister = findViewById(R.id.register);

        rfAuth = FirebaseAuth.getInstance();
        rprogressBar = findViewById(R.id.progressBar);

        if(rfAuth.getCurrentUser() != null){
            Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
            startActivity(intent);
            finish();
        }

        rRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String email = rEmail.getText().toString().trim();
                String password = rpassword.getText().toString().trim();

                if(TextUtils.isEmpty(email)){
                    rEmail.setError("Email is required");
                    return;
                }
                if(TextUtils.isEmpty(password)){
                    rpassword.setError("password is required");
                    return;
                }
                if(password.length() < 6){
                    rpassword.setError("password must be longer than 5 characters");
                    return;
                }

                rprogressBar.setVisibility(View.VISIBLE);

                // register the user to firebase

                rfAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(task.isSuccessful()){
                            Toast.makeText(MainActivity.this, "user created", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(getApplicationContext(), LoginActivity.class));
                        }
                        else
                            Toast.makeText(MainActivity.this, "Error ! " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                        rprogressBar.setVisibility(View.GONE);
                    }
                });
            }
        });

        rsignin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getApplicationContext(), LoginActivity.class));
            }
        });


    }
}

它没有显示任何错误,但我检查了 logcat 为什么会发生这种情况,这就是每次我单击创建帐户按钮时显示的内容:

2022-03-04 00:03:03.734 31199-31199/com.example.music_link_firebase D/DecorView: onWindowFocusChangedFromViewRoot hasFocus: true, DecorView@7893d4e[LoginActivity]
2022-03-04 00:03:03.735 31199-31199/com.example.music_link_firebase D/ViewRootImpl[LoginActivity]: windowFocusChanged hasFocus=true inTouchMode=true
2022-03-04 00:03:04.429 31199-31199/com.example.music_link_firebase E/c_link_firebas: Invalid ID 0x00000000.
2022-03-04 00:03:04.436 31199-31199/com.example.music_link_firebase E/c_link_firebas: Invalid ID 0x00000000.
2022-03-04 00:03:04.467 31199-31199/com.example.music_link_firebase D/ViewRootImpl[LoginActivity]: windowFocusChanged hasFocus=false inTouchMode=true
2022-03-04 00:03:04.522 31199-31199/com.example.music_link_firebase E/c_link_firebas: Invalid ID 0x00000000.
2022-03-04 00:03:04.544 31199-31199/com.example.music_link_firebase D/ViewRootImpl: support adaptive color gamut feature!
2022-03-04 00:03:04.529 31199-31199/com.example.music_link_firebase E/c_link_firebas: Invalid ID 0x00000000.
2022-03-04 00:03:04.545 31199-31199/com.example.music_link_firebase V/ViewRootImpl: The specified message queue synchronization  barrier token has not been posted or has already been removed
2022-03-04 00:03:04.584 31199-31199/com.example.music_link_firebase W/Choreographer: Already have a pending vsync event.  There should only be one at a time.
2022-03-04 00:03:04.605 31199-31199/com.example.music_link_firebase D/DecorView: onWindowFocusChangedFromViewRoot hasFocus: true, DecorView@dd70151[LoginActivity]
2022-03-04 00:03:04.606 31199-31199/com.example.music_link_firebase D/ViewRootImpl[LoginActivity]: windowFocusChanged hasFocus=true inTouchMode=true
2022-03-04 00:03:04.648 31199-4783/com.example.music_link_firebase W/System: A resource failed to call close. 
2022-03-04 00:03:04.648 31199-4783/com.example.music_link_firebase W/System: A resource failed to call close.

我关注了 youtube这段代码的教程,所以我不确定为什么会发生这种情况。另外,当我第一次运行该程序时,它首先将我引导至 MainActivity,即注册页面。在我注册并遇到问题后,每当我尝试停止程序并再次运行它时,它现在都会将我直接发送到 LoginActivity,这是不应该发生的。也不确定为什么。我还检查了 firebase 是否将我以前的注册插入到数据库中并且它在那里,所以我不太确定我哪里出错了。

When i click on my create account button, it should open another activity but instead, it just opens the same activity again and erases my inputs in the email and password. I also have my activities defined in the manifest.

package com.example.music_link_firebase;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

public class LoginActivity extends AppCompatActivity {

EditText lemail, lpassword;
Button login, lregister;
ProgressBar lprogressBar;
FirebaseAuth lfAuth;


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

    lemail = findViewById(R.id.email1);
    lpassword = findViewById(R.id.password1);
    login = findViewById(R.id.login1);
    lregister = findViewById(R.id.signup);
    lprogressBar = findViewById(R.id.progressBar2);
    lfAuth = FirebaseAuth.getInstance();

    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String email = lemail.getText().toString().trim();
            String password = lpassword.getText().toString().trim();

            if(TextUtils.isEmpty(email)){
                lemail.setError("Email is required");
                return;
            }
            if(TextUtils.isEmpty(password)){
                lpassword.setError("password is required");
            }
            if(password.length() < 6){
                lpassword.setError("password must be longer than 5 characters");
            }

            lprogressBar.setVisibility(View.VISIBLE);

            //authenticate the user

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

        }
    });

    lregister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(getApplicationContext(), MainActivity.class));
        }
    });




}

}

the java code for the activity that's supposed to open when the button is clicked

package com.example.music_link_firebase;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

public class MainActivity extends AppCompatActivity {

    EditText rEmail, rpassword, rphone, rfullname;
    Button rsignin, rRegister;
    FirebaseAuth rfAuth;
    ProgressBar rprogressBar;

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

        rfullname = findViewById(R.id.fullname);
        rEmail = findViewById(R.id.email);
        rpassword = findViewById(R.id.password);
        rphone = findViewById(R.id.phone);
        rsignin = findViewById(R.id.signin);
        rRegister = findViewById(R.id.register);

        rfAuth = FirebaseAuth.getInstance();
        rprogressBar = findViewById(R.id.progressBar);

        if(rfAuth.getCurrentUser() != null){
            Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
            startActivity(intent);
            finish();
        }

        rRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String email = rEmail.getText().toString().trim();
                String password = rpassword.getText().toString().trim();

                if(TextUtils.isEmpty(email)){
                    rEmail.setError("Email is required");
                    return;
                }
                if(TextUtils.isEmpty(password)){
                    rpassword.setError("password is required");
                    return;
                }
                if(password.length() < 6){
                    rpassword.setError("password must be longer than 5 characters");
                    return;
                }

                rprogressBar.setVisibility(View.VISIBLE);

                // register the user to firebase

                rfAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(task.isSuccessful()){
                            Toast.makeText(MainActivity.this, "user created", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(getApplicationContext(), LoginActivity.class));
                        }
                        else
                            Toast.makeText(MainActivity.this, "Error ! " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                        rprogressBar.setVisibility(View.GONE);
                    }
                });
            }
        });

        rsignin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getApplicationContext(), LoginActivity.class));
            }
        });


    }
}

it doesn't show any error but i checked the logcat why this is happening and this is what it shows everytime i click the create account button:

2022-03-04 00:03:03.734 31199-31199/com.example.music_link_firebase D/DecorView: onWindowFocusChangedFromViewRoot hasFocus: true, DecorView@7893d4e[LoginActivity]
2022-03-04 00:03:03.735 31199-31199/com.example.music_link_firebase D/ViewRootImpl[LoginActivity]: windowFocusChanged hasFocus=true inTouchMode=true
2022-03-04 00:03:04.429 31199-31199/com.example.music_link_firebase E/c_link_firebas: Invalid ID 0x00000000.
2022-03-04 00:03:04.436 31199-31199/com.example.music_link_firebase E/c_link_firebas: Invalid ID 0x00000000.
2022-03-04 00:03:04.467 31199-31199/com.example.music_link_firebase D/ViewRootImpl[LoginActivity]: windowFocusChanged hasFocus=false inTouchMode=true
2022-03-04 00:03:04.522 31199-31199/com.example.music_link_firebase E/c_link_firebas: Invalid ID 0x00000000.
2022-03-04 00:03:04.544 31199-31199/com.example.music_link_firebase D/ViewRootImpl: support adaptive color gamut feature!
2022-03-04 00:03:04.529 31199-31199/com.example.music_link_firebase E/c_link_firebas: Invalid ID 0x00000000.
2022-03-04 00:03:04.545 31199-31199/com.example.music_link_firebase V/ViewRootImpl: The specified message queue synchronization  barrier token has not been posted or has already been removed
2022-03-04 00:03:04.584 31199-31199/com.example.music_link_firebase W/Choreographer: Already have a pending vsync event.  There should only be one at a time.
2022-03-04 00:03:04.605 31199-31199/com.example.music_link_firebase D/DecorView: onWindowFocusChangedFromViewRoot hasFocus: true, DecorView@dd70151[LoginActivity]
2022-03-04 00:03:04.606 31199-31199/com.example.music_link_firebase D/ViewRootImpl[LoginActivity]: windowFocusChanged hasFocus=true inTouchMode=true
2022-03-04 00:03:04.648 31199-4783/com.example.music_link_firebase W/System: A resource failed to call close. 
2022-03-04 00:03:04.648 31199-4783/com.example.music_link_firebase W/System: A resource failed to call close.

I followed a youtube tutorial for this code so i'm not sure why this is happening. Also, when i first ran the program it directed me first to the MainActivity which is the registration page. after i registered and i encountered the problem, whenever i try to stop the program and run it again it now sends me directly to the LoginActivity which is not supposed to happen. not sure also why. I also checked on firebase if my previous registration was inserted in the database and it was there so i'm not really sure where i went wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文