简体   繁体   English

Firebase-创建帐户和登录问题

[英]Firebase - Create Account and Login issue

My app won't add users to Firebase Database. 我的应用程序不会将用户添加到Firebase数据库。 My database shows that three users' apps have crashed but the console doesn't show any users. 我的数据库显示三个用户的应用程序已崩溃,但控制台未显示任何用户。 Any help is appreciated. 任何帮助表示赞赏。

public class LoginPage extends AppCompatActivity implements View.OnClickListener
    {

    private FirebaseAuth mAuth;

    private DatabaseReference mDatabase;

    private static final String TAG = "GoogleActivity";

    private FirebaseAuth.AuthStateListener mAuthListener;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_page);
        mDatabase = FirebaseDatabase.getInstance().getReferenceFromUrl("https://student-help-portal.firebaseio.com");
        idusername = (EditText) findViewById(R.id.idusername);
        idpassword = (EditText) findViewById(R.id.idpassword);
        mAuth = FirebaseAuth.getInstance();
        buttonlogin = (Button) findViewById(R.id.buttonlogin);
        buttonsignup = (Button) findViewById(R.id.buttonsignup);
        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) {
                    // User is signed in
                    Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
                } else {
                    // User is signed out
                    Log.d(TAG, "onAuthStateChanged:signed_out");
                }
            }
        };
    }
    public class User {

        public String username;
        public String password;

        public User() {
            // Default constructor required for calls to DataSnapshot.getValue(User.class)
        }

        public User(String username, String password) {
            this.username = username;
            this.password = password;
        }

    }
    private String username, password;
    Button buttonlogin, buttonsignup;
    EditText idusername, idpassword;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login_page, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    private boolean validateForm() {
        boolean valid = true;
        if (TextUtils.isEmpty(username)) {
            idusername.setError("Required.");
            valid = false;
        } else {
            idusername.setError(null);
        }
        if (TextUtils.isEmpty(password)) {
            idpassword.setError("Required.");
            valid = false;
        } else {
            idpassword.setError(null);
        }

        return valid;
    }
    private void createAccount(String email, String password) {
        if (!validateForm()) {
            return;
        }
        Log.d(TAG, "createAccount:" + email);
        mAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful());
                        if (!task.isSuccessful()) {
                            Toast.makeText(LoginPage.this, "Failed to create account",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }
    @Override
    protected void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(mAuthListener);
        buttonsignup.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                username = idusername.getText().toString();
                password = idpassword.getText().toString();
                createAccount(username,password);
            }
        });
        buttonlogin.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                username = idusername.getText().toString();
                password = idpassword.getText().toString();
                signIn(username,password);
                Intent tohomepage = new Intent(LoginPage.this, HomePage.class);
                startActivity(tohomepage);
            }
        }
        );
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "LoginPage Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://curlybraces.studenthelpportal/http/host/path")
        );
        Action viewAction2 = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "LoginPage Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app URL is correct.
                Uri.parse("android-app://curlybraces.studenthelpportal/http/host/path")
        );
    }
    private void signIn(String username, String password) {
        Log.d(TAG, "signIn:" + username);
        if (!validateForm()) {
            return;
        }mAuth.signInWithEmailAndPassword(username, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        Log.d(TAG, "signInWithEmail:onComplete:" + task.isSuccessful());
                        if (!task.isSuccessful()) {
                            Log.w(TAG, "signInWithEmail:failed", task.getException());
                            Toast.makeText(LoginPage.this, "failed",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }
    private void signOut() {
        mAuth.signOut();
    }
    @Override
    public void onStop() {
        super.onStop();
        if (mAuthListener != null) {
            mAuth.removeAuthStateListener(mAuthListener);
        }
        /*Action viewAction2 = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "LoginPage Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app URL is correct.
                Uri.parse("android-app://curlybraces.studenthelpportal/http/host/path")
        );
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "LoginPage Page", // TODO: Define a title for the content shown.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://curlybraces.studenthelpportal/http/host/path")
        );*/
    }
    public String getUser(FirebaseUser user) {
        if (user != null) {
            return user.getEmail();
        }
        return null;
    }
    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.buttonsignup) {
            createAccount(username,password);
        } else if (i == R.id.buttonlogin) {
            signIn(username,password);
        } /*else if (i == R.id.sign_out_button) {
            signOut();
        }*/
    }

    }

I had the same problem and it was solved by changing the emulator API version. 我遇到了同样的问题,并且通过更改模拟器API版本解决了该问题。 I believe there is a problem between API 23 and Firebase. 我相信API 23和Firebase之间存在问题。 Changing it to 21 API the same code worked perfectly. 将其更改为21 API,相同的代码即可完美运行。

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

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