Firebase - 创建帐户和登录问题

时间:2016-09-06 20:34:40

标签: android firebase firebase-realtime-database firebase-authentication

我的应用无法将用户添加到Firebase数据库。我的数据库显示三个用户'应用程序已崩溃但控制台无法显示任何用户。任何帮助表示赞赏。

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

    }

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,通过更改模拟器API版本解决了这个问题。我相信API 23和Firebase之间存在问题。将其更改为21 API,相同的代码完美运行。