简体   繁体   中英

Firebase - Create Account and Login issue

My app won't add users to Firebase Database. 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. I believe there is a problem between API 23 and Firebase. Changing it to 21 API the same code worked perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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