简体   繁体   English

登录时出现Firebase NullPointException

[英]Firebase NullPointException when logging in

Hello im trying to create a firebase logging in auth with 2 different users, the admin, and user. 您好,我尝试使用2个不同的用户admin和user创建一个登录auth的firebase。 but like when i was trying to log in, The application would crash. 但是就像我尝试登录时,该应用程序将崩溃。 Heres the error i think 我认为这是继承人的错误 我认为的错误?

my database 我的数据库 这是我的数据库

and here is my code on the login 这是我登录时的代码

public class LoginActivity extends AppCompatActivity {
private EditText inputEmail, inputPassword;
private FirebaseAuth auth;
private ProgressBar progressBar;
private Button btnSignup, btnLogin, btnReset;
private DatabaseReference mFirebaseDatabase;
private FirebaseDatabase mFirebaseInstance;
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setIcon(R.mipmap.ic_launcher);
    auth = FirebaseAuth.getInstance();
    setContentView(R.layout.activity_login);


    inputEmail = (EditText) findViewById(R.id.email);
    inputPassword = (EditText) findViewById(R.id.password);
    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    btnSignup = (Button) findViewById(R.id.btn_signup);
    btnLogin = (Button) findViewById(R.id.btn_login);
    btnReset = (Button) findViewById(R.id.btn_reset_password);

    //db
    mFirebaseInstance = FirebaseDatabase.getInstance();

    //Get Firebase auth instance
    auth = FirebaseAuth.getInstance();
    btnSignup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(LoginActivity.this, signup.class));
        }
    });
    btnReset.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(LoginActivity.this, ResetPasswordActivity.class));
        }
    });
    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String email = inputEmail.getText().toString();
            final String password = inputPassword.getText().toString();

            if (TextUtils.isEmpty(email)) {
                Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
                return;
            }

            if (TextUtils.isEmpty(password)) {
                Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
                return;
            }

            progressBar.setVisibility(View.VISIBLE);
            //authenticate user
            auth.signInWithEmailAndPassword(email, password)
                    .addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            // If sign in fails, display a message to the user. If sign in succeeds
                            // the auth state listener will be notified and logic to handle the
                            // signed in user can be handled in the listener.
                            progressBar.setVisibility(View.GONE);
                            if (!task.isSuccessful()) {
                                // there was an error
                                if (password.length() < 6) {
                                    inputPassword.setError(getString(R.string.minimum_password));
                                } else {
                                    Toast.makeText(LoginActivity.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
                                }
                            } else {
                                onAuthSuccess(task.getResult().getUser());

                            }
                        }

                        private void onAuthSuccess(FirebaseUser user) {
                            if (user !=null){
                                mFirebaseDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(user.getUid()).child("type");
                                mFirebaseDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
                                    @Override
                                    public void onDataChange(DataSnapshot dataSnapshot) {
                                        String value = dataSnapshot.getValue(String.class);
                                        if(Integer.parseInt(value) == 1) {
                                            startActivity(new Intent(LoginActivity.this, UserActivity.class));
                                            Toast.makeText(LoginActivity.this, "Welcome User", Toast.LENGTH_SHORT).show();
                                            finish();
                                        }else {
                                            startActivity(new Intent(LoginActivity.this, AdminActivity.class));
                                            Toast.makeText(LoginActivity.this, "Welcome", Toast.LENGTH_SHORT).show();
                                            finish();
                                        }

                                    }

                                    @Override
                                    public void onCancelled(DatabaseError databaseError) {

                                    }
                                });

                            }
                        }
                    });
        }
    });
}

I feel like the problem lies on the ondata change one but I don't know what to do to fix this, so im asking for your help :O 我觉得问题出在ondata更改之一上,但是我不知道该怎么做才能解决此问题,所以我向您寻求帮助:O

Try this: 尝试这个:

  mFirebaseDatabase = FirebaseDatabase.getInstance().getReference().child("Accounts").child("Users").child(user.getUid());
                            mFirebaseDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {
                                    String value = dataSnapshot.child("type").getValue(String.class);
                                    if(Integer.parseInt(value) == 1) {
                                        startActivity(new Intent(LoginActivity.this, UserActivity.class));
                                        Toast.makeText(LoginActivity.this, "Welcome User", Toast.LENGTH_SHORT).show();
                                        finish();
                                    }else {
                                        startActivity(new Intent(LoginActivity.this, AdminActivity.class));
                                        Toast.makeText(LoginActivity.this, "Welcome", Toast.LENGTH_SHORT).show();
                                        finish();
                                    }

                                }

try the above, have the reference at the userid then inside onDataChange retrieve the type . 尝试上述操作,在userid处具有引用,然后在onDataChange内检索type you need to reference in order and specify the child name like this: 您需要按顺序引用并指定子名称,如下所示:

String value = dataSnapshot.child("type").getValue(String.class);

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

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